条件和循环:如何在多次调用函数时不恢复音乐? [英] condition and loop: how can i don't resume the music when calling the function several times?

查看:127
本文介绍了条件和循环:如何在多次调用函数时不恢复音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能用网络音频API播放音乐:

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();}

 }

但是,count和countPre的值是在每秒运行10次的循环中生成的。

However, the value of count and countPre are generated in a loop that runs 10 times per second.

我有将函数playMusic 放在循环中以更新值。

I have to put the function playMusic inside that loop in order to update the values.

问题出现了:

我每秒呼叫playMusic 10次!每次,音乐恢复

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?

推荐答案

您必须检查音乐是否已经在您的状态下播放。

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}
 }

这篇关于条件和循环:如何在多次调用函数时不恢复音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆