HTML5音频 - 声音只播放一次 [英] HTML5 Audio - Sound only plays once
本文介绍了HTML5音频 - 声音只播放一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< audio id =letter_audiosrc =audio / bomb.mp3type =audio / mp3preload =auto>< / audio>
$ b $('。sound')。live('click',function(){
var sound = $(#letter_audio)[0];
$ b sound.pause();
sound.currentTime = 0;
sound.play();
return false;
});
我第一次点击播放时声音如何播放。然后当我再次点击时,它会暂停,就像代码一样。但它只是暂停?我想让它播放,然后当我再次点击它时,回到0并再次播放代码?
>我有类似的问题重放音频,但设法通过调用 load()
方法在 play()
呼叫。试试这个: $('。sound')。live('click',function(){
var声音= $(#letter_audio)[0];
sound.load();
sound.play();
返回false;
});
我认为这比重新设定 src
- 保留页面内容中的
src
值,而不是在JavaScript中嵌入不同音频格式的多个< source>
元素时的Works
< audio id =letter_audio>
< source src =audio / bomb.mp3type =audio / mp3preload =auto/>
< source src =audio / bomb.oggtype =audio / oggpreload =auto/>
< / audio>
I am using Chrome and this is my code:
<audio id="letter_audio" src="audio/bomb.mp3" type="audio/mp3" preload="auto"></audio>
$('.sound').live('click', function () {
var sound = $("#letter_audio")[0];
sound.pause();
sound.currentTime = 0;
sound.play();
return false;
});
How come the sound plays the first time I click on play. then when I click again, it pauses, like the code goes. but the it justs stays on pause? i want it to play, then when i click on it again, go back to 0 and play again as in the code?
解决方案
I've had similar problems replaying audio, but managed to solve it by calling the load()
method before the play()
call. Try this:
$('.sound').live('click', function () {
var sound = $("#letter_audio")[0];
sound.load();
sound.play();
return false;
});
I think this is better than resetting the src
attribute for two reasons:
- Keeps the
src
value in the page content, rather than in the JavaScript Works when you embed multiple
<source>
elements of different audio formats:<audio id="letter_audio"> <source src="audio/bomb.mp3" type="audio/mp3" preload="auto"/> <source src="audio/bomb.ogg" type="audio/ogg" preload="auto"/> </audio>
这篇关于HTML5音频 - 声音只播放一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文