Condition and loop: how can I not resume music when calling a function several times?

I have this function to play music using the 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, count and countPre are generated in a loop that starts 10 times per second.

I need to put the playMusic function inside in this loop to update the values.

And here is the problem:

I call playMusic 10 times per second! Each time the music resumes .

I do not want it to resume, I want it to play continuously until the playback condition matches.

So is there any solution?

0
source share
1 answer

You must verify that the music is not yet playing in your state.

Do not think that there is a sound method for this. You can do this in many ways, for 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} } 
0
source

All Articles