phonegap media sound stop playing after 5 times

2019-02-07 00:52发布

问题:

I need to play the "correct" sound if user answer the question correctly and "wrong" if incorrect. The sound is playing at first but after at the fifth time I answer correctly which output the fourth time "correct" sound already the fifth time, no sound can be heard. It seems the music can only played at most 4th times. What is the possible reason that cause this?

updated* I added media.release in my stopAudio() but so far the sound can be heard for 6th (improved) but it is still not suits my case. (Many questions that need the same sound effect)

Js.file

// Audio player
    var my_media = null;
    var mediaTimer = null;

    // Play audio
    function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, null, soundCB);
        my_media.play();
    }

    function soundCB(err){
        console.log("playAudio():Audio Error: "+err);
    }

    // Stop audio
    // 
    function stopAudio() {
        if (my_media) {
            alert("AQA");
            my_media.stop();
            my_media.release();
        }
        clearInterval(mediaTimer);
        mediaTimer = null;
    }

/////question part//////

$("#answer").live('tap', function(event){   

if(buttonAns==true) {
    $this= $(this);     
    buttonAns = false;   
var choice = $this.attr("class");   
if((correctAnsNo == 0 && choice =="answer0")||(correctAnsNo == 1 && choice =="answer1")||(correctAnsNo == 2 && choice =="answer2")){

        playAudio('/android_asset/www/audio/fall.mp3'); //falling sound     
        correct = parseInt(correct)+2;  

        playAudio('/android_asset/www/audio/correct.mp3'); //dingdong
    }else{          
        playAudio('/android_asset/www/audio/wrong1.wav');           
    }
    if(quesNo < wordsNo ){ 
        setTimeout(function() {    
            buttonAns = true;                       
            db.transaction(queryQuesDB, errorCB);
        },1800); 
    }else{
        endLevel();         
    } 
} else {
    event.preventDefault();
}       
});

回答1:

Android has a finite amount of resources in order to play sounds. You keep creating a new Media object each time you want to play a file. You need to release the sounds you are not using. If you need to play those sounds multiple times each then consider creating separate media objects to hold a reference to those sounds.

Read up on media.release.



回答2:

I had the same problem. The solution I did.:

     if(media){ 
       media.stop();
       media.release();
     }
     media = new Media(...);


回答3:

Here's my trick: I released it upon it finished playing or an error occurred.

function playAudio(url) {
    try {

        var my_media = new Media(url,
            // success callback
            function () {
                **my_media.release();**
            },
            // error callback
            function (err) {
                **my_media.release();**
            });

        // Play audio
        my_media.play();
    } catch (e) {
        alert(e.message);
    }
}