Soundtrack sound after 5 times

I need to play the “correct” sound if the user answers the question correctly and “incorrectly” if it is incorrect. The sound is played at first, but for the fifth time I answer correctly, which outputs the “correct” sound for the fourth time for the fifth time, the sound is not heard. It seems that music can only play 4 times. What is the possible reason for this?

updated * I added media.release to my stopAudio (), but so far the sound can be heard on the 6th (improved), but it still doesn't fit my case. (Many questions that require 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(); } }); 
+7
source share
3 answers

Android has a limited amount of resources for playing sounds. You are constantly creating a new Media object every time you want to play a file. You need to release sounds that you are not using. If you need to play these sounds several times, then consider creating separate media objects to maintain a link to these sounds.

Read on media.release .

+8
source

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

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

Here is my trick: I released it when I finished the game 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); } } 
+5
source

All Articles