Search code examples
javascriptcordovaionic-frameworkcordova-pluginsphonegap

Cordova Media Plugin multiple audio dosent work


i am new to cordova i tired to find solution ,i have 20 sound into my app , when i run it the app run first clicked Audio only and ignore/reject another sounds Code:

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
    document.querySelector("#playMp3Mild").addEventListener("touchend", playMp3Mild, false);
    document.querySelector("#stop").addEventListener("touchend", stopAudio, false);
    document.querySelector("#pause").addEventListener("touchend", pauseAudio, false);

};

media = null;
mediaTimer = null;


function playMp3Mild(src) {
    mp3URL = getMediaURL(src);
    if(media === null){
    media = new Media(mp3URL , null, mediaError);
    }
    media.setVolume(0.1);
    media.play();
}

      function stopAudio() {
            if (media) {
                media.stop();
            }
            clearInterval(mediaTimer);
            mediaTimer = null;
        }


 function getMediaURL(s) {
     if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s;
     return s;
 }

function mediaError(e) {

}

i try remove this line if(media === null) it's work , but now i haved another problem , all audios working together in same time. so sorry for my bad english i believe with the community will be more good


Solution

  • You might want to try stopping the old media first because you overwrite the media variable and not stopping the old one. Then also set he old media value to null so your if(media === null) statement will return true. media.stop() does not make media equal to null. Please try the following and tell us if that worked for you.

    function playMp3Mild(src) {
        mp3URL = getMediaURL(src);
        stopAudio();
        if(media === null){
        media = new Media(mp3URL , null, mediaError);
        }
        media.setVolume(0.1);
        media.play();
    }
    
    function stopAudio() {
        if (media) {
            media.stop();
        }
        clearInterval(mediaTimer);
        media = null;
        mediaTimer = null;
    }