Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phonegap media sound stop playing after 5 times

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();
}       
});
like image 781
shoujo_sm Avatar asked Nov 26 '12 13:11

shoujo_sm


2 Answers

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.

like image 191
Simon MacDonald Avatar answered Sep 24 '22 04:09

Simon MacDonald


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

     if(media){ 
       media.stop();
       media.release();
     }
     media = new Media(...);
like image 42
Enacefio Avatar answered Sep 21 '22 04:09

Enacefio