问题描述
如果用户回答正确,我需要播放正确"的声音,如果不正确,我需要播放错误"的声音.声音一开始播放,但在第五次我正确回答之后,已经第五次输出第四次正确"的声音,听不到声音.似乎音乐最多只能播放第四遍.造成这种情况的可能原因是什么?
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?
更新*我在我的 stopAudio() 中添加了 media.release 但到目前为止可以听到第 6 次(改进)的声音,但它仍然不适合我的情况.(很多问题需要同样的音效)
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();
}
});
推荐答案
Android 拥有有限数量的资源来播放声音.每次要播放文件时,都会不断创建一个新的 Media 对象.你需要释放你不使用的声音.如果您需要多次播放这些声音,请考虑创建单独的媒体对象来保存对这些声音的引用.
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.
这篇关于phonegap 媒体声音在 5 次后停止播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!