condition and loop: how can i don't resume the music when calling the function several times?

一曲冷凌霜 提交于 2019-12-08 10:34:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!