简体   繁体   English

如何在不重叠的情况下停止并播放HTML5中的多种声音?

[英]How to stop and play multiple sounds in HTML5 without overlapping them?

I'm using javascript Audio class to play different audio files in background in an HTML5 application. 我正在使用JavaScript Audio类在HTML5应用程序中在后台播放不同的音频文件。
Every audio file is bound to the click event of a button. 每个音频文件都绑定到按钮的click事件。

Now what I want to achieve is that when a button is clicked, the current audio in progress should stop immediately and the one bound to that specific button should start. 现在我想要实现的是,当单击一个按钮时,正在进行的当前音频应立即停止,并且应该开始绑定到该特定按钮的音频。

I've tried to use pause() and mute() methods but in both cases the audios overlap for a very small instance of time (<=1 second). 我尝试使用pause()Mutant()方法,但是在两种情况下,音频重叠的时间都非常小(<= 1秒)。

Is there any solution for this problem? 这个问题有什么解决办法吗? Below is the code I'm using currently: 以下是我当前使用的代码:

  if (sound === null) { sound = new Audio("sounds/" + audioId + ".mp3"); sound.play(); } else { sound.pause(); sound.src = "sounds/" + audioId + ".mp3"; sound.play(); } 

You may use played() this function Sets or returns if the audio/video is paused or not. 您可以使用played()此函数设置音频/视频是否暂停。

Firstly , you pause the audio and then check if audio is paused or not using paused() after that play sound,instead of play audio just after pause. 首先 ,您暂停音频,然后在播放声音后检查音频是否已暂停或不使用paused() ,而不是在暂停后立即播放音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM