[英]Check if audio is playing without HTML5 tag
I want to detect if an audio is playing, in case it is, don't allow to play it again until it finish.我想检测音频是否正在播放,如果是,则在播放完成之前不允许再次播放。 Play the same audio twice causes an audio bug.两次播放相同的音频会导致音频错误。
HTML HTML
<button onclick="play()">Play</button> Press two times
Javascript: Javascript:
function play() {
var sound = new Audio('https://mfaucet.com/images/notification2.mp3');
console.log('Paused: ' + sound.paused);
console.log('Ended: ' + sound.ended);
console.log('Current time: ' + sound.currentTime);
sound.play();
console.log('-----------------');
console.log('Paused: ' + sound.paused);
console.log('Ended: ' + sound.ended);
console.log('Current time: ' + sound.currentTime);
console.log('-----------------------------');
}
Online: http://jsbin.com/hageko/1/在线: http : //jsbin.com/hageko/1/
Thanks.谢谢。
This should work:这应该有效:
var sound = new Audio('https://mfaucet.com/images/notification2.mp3'); function play(sound) { if(!sound.paused) sound.pause(); sound.currentTime = 0; sound.play(); }
<button onclick="play(sound)">Play</button>Press two times
Explanation:解释:
The reason you are getting that is that you are creating a new instance of the audio object every time.你得到它的原因是你每次都在创建音频对象的一个新实例。 The Best way to prevent this is to save your instance somewhere.防止这种情况的最佳方法是将您的实例保存在某处。 Heres how you can make it work:以下是如何使其工作:
/* Store your sound globally */
var sound = false;
function play() {
/* Check if the sound is not instantiated yet */
if(!sound){
/* If its not instantiated, instantiate it here */
sound = new Audio('https://mfaucet.com/images/notification2.mp3');
}
sound.play();
}
That should fix your issue.那应该可以解决您的问题。 However, a better way would be to add it to the DOM itself instead as your button is part of the DOM anyway (and your button would be useless if there isn;t an audio element).但是,更好的方法是将它添加到 DOM 本身,因为您的按钮无论如何都是 DOM 的一部分(如果没有音频元素,您的按钮将毫无用处)。 That way you separate the issue of making something play and you could do it as such:这样你就可以把玩游戏的问题分开,你可以这样做:
<audio id="myAudio">
<source src="https://mfaucet.com/images/notification2.mp3" />
</audio>
<button onclick="document.getElementById('myAudio').play()">Play</button>
This would save you adding any more javascript to than needed as play is a built-in function anyhow.这将节省您添加比需要更多的 javascript,因为无论如何 play 是一个内置函数。 Now adding a pause button is trivial:现在添加一个暂停按钮是微不足道的:
<button onclick="document.getElementById('myAudio').pause()">Pause</button>
Actually, adding any standard event is trivial.实际上,添加任何标准事件都是微不足道的。 Heres a list of things you can do on this object referenced by id: https://developer.mozilla.org/en/docs/Web/API/HTMLMediaElement以下是您可以对 id 引用的此对象执行的操作列表: https : //developer.mozilla.org/en/docs/Web/API/HTMLMediaElement
Use this:用这个:
var sound = new Audio('https://mfaucet.com/images/notification2.mp3'); var neverPlayed = true; function play() { if(sound.ended || neverPlayed) { neverPlayed = false; console.log('Paused: ' + sound.paused); console.log('Ended: ' + sound.ended); console.log('Current time: ' + sound.currentTime); sound.play(); console.log('-----------------'); console.log('Paused: ' + sound.paused); console.log('Ended: ' + sound.ended); console.log('Current time: ' + sound.currentTime); console.log('-----------------------------'); } else { console.log('Sound running'); } }
<button onclick="play()">Play</button>
Here A Code who checks with cookies if the Sound is ON or OFF这里有一个代码,用于检查声音是否打开或关闭
You need to add the a js-Cookie and jQuery Librarys:您需要添加一个js-Cookie和jQuery库:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
// Set a cookie
Cookies.set('click_play', 'OFF');
//Audio Source
var audio = new Audio("http://rs01.swissnwx.ch:20080/stream");
//Audio Starts on first Click
document.onclick = function() {
if (Cookies.get('click_play')== 'OFF') {
//Audio was OFF so now will be turned ON
audio.play();
console.log('play');
Cookies.set('click_play', 'ON');
}
else {
//Audio was ON so now will be turned OFF
audio.pause();
console.log('stop');
Cookies.set('click_play', 'OFF');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.