Media Playback on iOS

10 views
Skip to first unread message

das licht

unread,
May 30, 2017, 4:32:10 PM5/30/17
to phonegap
Hi,
i have the following code which works in the browser.

What I tried so far:

play sound from external source (http:// ...) -> works iunder iOS
play local wav file by providing a relative path  -> fail no playback
play local mp3 file by providing a relative path  -> fail no playback

I also tried providing a absolute path that didn't work either.

The audio files song.wav and song.mp3 reside just in the root /www folder.

I also tried playing them somewhere else -> fail

What am I missing please?

~ Marc



var button = document.querySelector('#play');


/**
 * I used those function to ty out absolute path, result : no playback
 */
var getPhoneGapPath = function () {
    'use strict';
    var path = window.location.pathname;
    var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
    return phoneGapPath;
}

// function getPhoneGapPath() {
//     var path = window.location.pathname;
//     path = path.substr( 0, path.length - 10 );
//     return 'file://' + path;
// };

// mp3 or wav, both are not playing under iOS
var resource = getPhoneGapPath() + 'song.wav';

button.addEventListener("click", function() {
  console.log('resource', resource);
  playAudio( resource  );
})


function playAudio(url) {

    var my_media = new Media(url,
        function () {
            console.log("playAudio():Audio Success");
        },
        function (err) {
            console.log("playAudio():Audio Error: ", err);
        }
    );
    console.log('my_media: ', my_media);
    my_media.play();
}




var app = {

    initialize: function() {
        this.bindEvents();
    },

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        var source = 'song.mp3'
    
        playAudio( source  );
        // opening an url seams to work on iOS !
    },

    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};


Reply all
Reply to author
Forward
0 new messages