问题
i have this function to play music with web audio API:
function playMusic(){
if(countPre<count ){
audio0.play();
audio0.src = '0.mp3';
audio0.controls = true;
audio0.autoplay = true;
audio0.loop = true;
source = context.createBufferSource();
source.connect(analyser);
analyser.connect(context.destination);
}
else{audio0.pause();}
}
However, the value of count and countPre are generated in a loop that runs 10 times per second.
I have to put the function playMusic inside that loop in order to update the values.
And here comes the problem:
I call playMusic 10 times per second! Every time, the music resumes.
I don't want it resumes, i want it plays continuously as long as the play condition is matched.
So is there any solution?
回答1:
You have to check that music is not already playing in your condition.
Don't think there's an audio method for that. You can do it by many ways, example :
function playMusic(){
if(countPre<count && !this.is_playing ){
this.is_playing = true;
audio0.play();
audio0.src = '0.mp3';
audio0.controls = true;
audio0.autoplay = true;
audio0.loop = true;
source = context.createBufferSource();
source.connect(analyser);
analyser.connect(context.destination);
}
else{audio0.pause();
this.is_playing = false}
}
来源:https://stackoverflow.com/questions/13117654/condition-and-loop-how-can-i-dont-resume-the-music-when-calling-the-function-s