I've been using this code from the PhoneGap documentation (http://docs.phonegap.com/en/2.3.0/cordova_media_media.md.html#media.play) to play audio files:
// Audio player
//
var my_media = null;
var mediaTimer = null;
// Play audio
//
function playAudio(src) {
if (my_media == null) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
} // else play current audio
// Play audio
my_media.play();
// Stop audio
//
function stopAudio() {
if (my_media) {
my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
So I can playback audio with an onclick event:
playAudio('http://example.com/file.mp3');
And stop it:
stopAudio();
That's working fine, but I want to play multiple streams and control them by a parameter, "name".
So I changed my code:
// Audio player
//
var my_media = null;
var mediaTimer = null;
// Play audio
//
function playAudio(name,src) {
if (my_media == null) {
// Create Media object from src
my_media = new Media(name, src, onSuccess, onError);
} // else play current audio
// Play audio
my_media.play(name);
// Stop audio
//
function stopAudio(name) {
if (my_media) {
my_media.stop(name);
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// Play and stop file 1: playAudio('file1','http://example.com/file.mp3');
stopAudio('file1');
// Play and stop file 2:
playAudio('file2','http://example.com/file2.mp3');
stopAudio('file2');
The only thing I'm receiving is a crash of the application, does anyone have experiences with playing multiple media files on PhoneGap?
Thanks!