简体   繁体   English

使用JavaScript停止所有HTML5音频/视频

[英]Stop all HTML5 audio/video with JavaScript

Trying to write a function that will pause all HTML5 audio and video. 尝试编写将暂停所有HTML5音频和视频的功能。

I've got this far, but I'm not sure if this is the correct way about it? 我已经走了这么远,但是我不确定这是否是正确的方法?

function stopAll() {
var media = document.getElementsByClassName('media'),
    i = media.length;

while (i--) {
    media[i].pause();
}
}

http://jsfiddle.net/m6AkC/ http://jsfiddle.net/m6AkC/

That works. 这样可行。 In your fiddle, you have to make sure that the source tags are closed and that no wrap - in is selected. 在摆弄时,您必须确保关闭源标签,并且未选择任何自动换行。

The problem was really in your fiddle. 问题确实出在您的摆弄上。 You need to make sure that no wrap - in head is selected as the option on the upper left under Frameworks & Extensions. 您需要确保在Frameworks&Extensions下左上角的选项中没有选择“自动换行 ”。 See the updated fiddle at @ http://jsfiddle.net/m6AkC/2/ . 请访问@ http://jsfiddle.net/m6AkC/2/查看更新的提琴。

<audio class="media" id="audioplayer" controls>
<source src="http://download.wavetlan.com/SVV/Media/HTTP/BlackBerry.mp4" type="audio/mpeg">
    <source src="audio/macdemarco.ogg" type="audio/ogg">Your browser does not support this audio format.</audio>
<video class="media" width="340" height="200" controls>
<source src="http://download.wavetlan.com/SVV/Media/HTTP/BlackBerry.mp4" type="video/mp4">
    <source src="movie.ogg" type="video/ogg">Your browser does not support the video tag.</video>
<br>
<a href="#" onclick="stopAll()" id="next">Stop</a>

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

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