我正在使用YouTube iframe在我的网站上嵌入视频。
<iframe width="100%" height="443" class="yvideo" id="p1QgNF6J1h0"
src="http://www.youtube.com/embed/p1QgNF6J1h0?rel=0&controls=0&hd=1&showinfo=0&enablejsapi=1"
frameborder="0" allowfullscreen>
</iframe>
我在同一页面上有多个视频。
我希望使用javascript左键单击按钮停止所有这些或其中一个。
有可能吗?
我尝试了 Talvi Watia 所说和使用的内容:
$('#myStopClickButton').click(function(){
$('.yvideo').each(function(){
$(this).stopVideo();
});
});
我得到了:
Uncaught TypeError: Object [object Object] has no method 'stopVideo'
答案 0 :(得分:58)
不幸的是,这些API的发展速度非常快。截至2015年5月,建议的解决方案不再起作用,因为播放器对象没有stopVideo
方法。
可以在此页面中找到可靠的解决方案(1),它可用于:
<iframe id="youtube_player" class="yt_player_iframe" width="640" height="360" src="http://www.youtube.com/embed/aHUBlv5_K8Y?enablejsapi=1&version=3&playerapiid=ytplayer" allowfullscreen="true" allowscriptaccess="always" frameborder="0"></iframe>
以及以下JS / jQuery代码:
$('.yt_player_iframe').each(function(){
this.contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*')
});
答案 1 :(得分:30)
如果有人还在寻找答案,我已经解决了这个问题:
$("#photos").on("hide",function(){
var leg=$('.videoPlayer').attr("src");
$('.videoPlayer').attr("src",leg);
});
其中#photos是模态的ID,而.videoPlayer是iframe的类。 基本上它会刷新src属性(并停止播放视频)。 所以,
$('#myStopClickButton').click(function(){
$('.yvideo').each(function(){
var el_src = $(this).attr("src");
$(this).attr("src",el_src);
});
});
应该这样做。
答案 2 :(得分:13)
Talvi的回答可能仍然有效,但Youtube Javascript API已被标记为已弃用。您现在应该使用较新的Youtube IFrame API。
文档提供了一些完成视频嵌入的方法,但为了您的目标,您需要包含以下内容:
//load the IFrame Player API code asynchronously
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
//will be youtube player references once API is loaded
var players = [];
//gets called once the player API has loaded
function onYouTubeIframeAPIReady() {
$('.myiframeclass').each(function() {
var frame = $(this);
//create each instance using the individual iframe id
var player = new YT.Player(frame.attr('id'));
players.push(player);
});
}
//global stop button click handler
$('#mybutton').click(function(){
//loop through each Youtube player instance and call stopVideo()
for (var i in players) {
var player = players[i];
player.stopVideo();
}
});
答案 3 :(得分:12)
API很混乱,因为它们不断变化。这种纯粹的javascript方式对我有用:
<div id="divScope" class="boom-lightbox" style="display: none;">
<iframe id="ytplayer" width="720" height="405" src="https://www.youtube.com/embed/M7lc1UVf-VE" frameborder="0" allowfullscreen> </iframe>
</div>
//if I want i can set scope to a specific region
var myScope = document.getElementById('divScope');
//otherwise set scope as the entire document
//var myScope = document;
//if there is an iframe inside maybe embedded multimedia video/audio, we should reload so it stops playing
var iframes = myScope.getElementsByTagName("iframe");
if (iframes != null) {
for (var i = 0; i < iframes.length; i++) {
iframes[i].src = iframes[i].src; //causes a reload so it stops playing, music, video, etc.
}
}
答案 4 :(得分:11)
您可能需要查看Youtube JavaScript API参考文档。
当您在网页上嵌入视频时,您需要传递此参数:
http://www.youtube.com/v/VIDEO_ID?version=3&enablejsapi=1
如果你想要停止所有视频按钮,你可以设置一个javascript例程来循环播放你的视频并停止它们:
player.stopVideo()
这确实涉及跟踪页面上每个视频的所有页面ID。更简单的可能是创建一个类,然后使用jQuery.each。
$('#myStopClickButton').click(function(){
$('.myVideoClass').each(function(){
$(this).stopVideo();
});
});
答案 5 :(得分:9)
有一个来自名为“ CAPTAIN ANONYMOUS”的用户的编码笔。它对我有用-但我必须在应有的位置注明信誉-我在这里发布是因为我正在寻找将YT视频嵌入到iframe。而且我觉得发布它可以帮助其他人减少搜索时间。
我需要的是使视频出现在模式窗口中,并在关闭时停止播放
这就是魔术: https://codepen.io/anon/pen/GBjqQr
<div><a href="#" class="play-video">Play Video</a></div>
<div><a href="#" class="stop-video">Stop Video</a></div>
<div><a href="#" class="pause-video">Pause Video</a></div>
<iframe class="youtube-video" width="560" height="315" src="https://www.youtube.com/embed/glEiPXAYE-U?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allowfullscreen></iframe>
$('a.play-video').click(function(){
$('.youtube-video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'playVideo' + '","args":""}', '*');
});
$('a.stop-video').click(function(){
$('.youtube-video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
});
$('a.pause-video').click(function(){
$('.youtube-video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*');
});
此外,如果您希望它在尚不可见的dom对象(例如模态窗口)中自动播放,如果我使用相同的按钮来播放用于显示模态的视频,那么它将无法正常工作我用的是这个
https://www.youtube.com/embed/EzAGZCPSOfg?autoplay=1&enablejsapi=1&version=3&playerapiid=ytplayer
注意:?autoplay = 1&放置在其中,并在下一个属性之前使用'&',以允许暂停继续进行。
答案 6 :(得分:5)
不能简单地高估这篇文章并回答OP和助手的问题。 我的解决方案只是video_id交换:
<div style="pointer-events: none;">
<iframe id="myVideo" src="https://www.youtube.com/embed/video_id?rel=0&modestbranding=1&fs=0&controls=0&autoplay=1&showinfo=0&version=3&enablejsapi=1" width="560" height="315" frameborder="0"></iframe> </div>
<button id="play">PLAY</button>
<button id="pause">PAUSE</button>
<script>
$('#play').click(function() {
$('#myVideo').each(function(){
var frame = document.getElementById("myVideo");
frame.contentWindow.postMessage(
'{"event":"command","func":"playVideo","args":""}',
'*');
});
});
$('#pause').click(function() {
$('#myVideo').each(function(){
var frame = document.getElementById("myVideo");
frame.contentWindow.postMessage(
'{"event":"command","func":"pauseVideo","args":""}',
'*');
});
});
</script>
答案 7 :(得分:4)
最简单的方法是
var frame = document.getElementById("iframeid");
frame.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
答案 8 :(得分:1)
另见How to pause or stop an iframe youtube video when you leave a tab view or minimise your Ionic App
$scope.stopVideo = function() {
var iframe = document.getElementsByTagName("iframe")[0].contentWindow;
iframe.postMessage('{"event":"command","func":"'+'stopVideo'+ '","args":""}', '*');
}
答案 9 :(得分:1)
$('#aboutVideo .close').on('click',function(){
var reSrc = $('.aboutPlayer').attr("src");
$('.aboutPlayer').attr("src",reSrc)
})
#aboutVideo{
width: 100%;
height: 100%;
}
#aboutVideo .modal-dialog, #aboutVideo .modal-dialog .modal-content, #aboutVideo .modal-dialog .modal-content .modal-body{
width: 100%;
height: 100%;
margin: 0 !important;
padding: 0 !important;
}
#aboutVideo .modal-header{
padding: 0px;
border-bottom: 0px solid #e5e5e5;
position: absolute;
right: 4%;
top: 4%;
}
#aboutVideo .modal-header .close{
opacity: 1;
position: absolute;
z-index: 99;
color: #fff;
}
#aboutVideo .modal-header button.close{
border-radius: 50%;
width: 7vw;
height: 7vw;
position: absolute;
right: 4%;
top: 7%;
background: aliceblue;
}
#aboutVideo .modal-header button.close:hover{
background-color: rgba(255, 255, 255, 0.28);
}
#aboutVideo .modal-header button.close img{
width: 20px;
margin-top: -0.2vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<li class="see-video fa" type="button" data-toggle="modal" data-target="#aboutVideo">
<label>SEE VIDEO</label>
</li>
<div class="modal fade" id="aboutVideo" tabindex="-1" role="dialog" aria-labelledby="aboutVideoLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><img src="http://www.freeiconspng.com/uploads/white-close-button-png-16.png"></span></button>
</div>
<div class="modal-body">
<iframe class="aboutPlayer" width="100%" height="100%" src="https://www.youtube.com/embed/fju9ii8YsGs?autoplay=0&showinfo=0&controls=2&rel=0" frameborder="0" allowfullscreen poster="https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiOvaagmqfWAhUHMY8KHUuJCnkQjRwIBw&url=http%3A%2F%2Fnodeframework.com%2F&psig=AFQjCNEaHveDtZ81veNPSvQDx4IqaE_Tzw&ust=1505565378467268"></iframe>
</div>
</div>
</div>
</div>
答案 10 :(得分:1)
这是一个纯JavaScript解决方案,
<iframe
width="100%"
height="443"
class="yvideo"
id="p1QgNF6J1h0"
src="http://www.youtube.com/embed/p1QgNF6J1h0?rel=0&controls=0&hd=1&showinfo=0&enablejsapi=1"
frameborder="0"
allowfullscreen>
</iframe>
<button id="myStopClickButton">Stop</button>
<script>
document.getElementById("myStopClickButton").addEventListener("click", function(evt){
var video = document.getElementsByClassName("yvideo");
for (var i=0; i<video.length; i++) {
video.item(i).contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*');
}
});
答案 11 :(得分:1)
对于内置视频的Twitter Bootstrap模态/弹出窗口,这对我有用:
$('.modal.stop-video-on-close').on('hidden.bs.modal', function(e) {
$('.video-to-stop', this).each(function() {
this.contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div id="vid" class="modal stop-video-on-close"
tabindex="-1" role="dialog" aria-labelledby="Title">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
<iframe class="video-to-stop center-block"
src="https://www.youtube.com/embed/3q4LzDPK6ps?enablejsapi=1&rel=0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
frameborder="0" allowfullscreen>
</iframe>
</div>
<div class="modal-footer">
<button class="btn btn-danger waves-effect waves-light"
data-dismiss="modal" type="button">Close</button>
</div>
</div>
</div>
</div>
<button class="btn btn-success" data-toggle="modal"
data-target="#vid" type="button">Open video modal</button>
基于Marco's answer,请注意,我只需要向视频URL添加enablejsapi=1
参数(rel=0
仅用于不显示相关视频)。 JS postMessage
函数是所有繁重的工作,实际上可以停止视频。
由于请求权限,该代码段可能不会显示视频,但在常规浏览器中,此代码应于2018年11月开始运行。
答案 12 :(得分:0)
如果任何人都想在不使用jQuery的情况下停止视频,则只需:
iframe.src = '';
缺点是您必须跟踪该URL。但这通常不会有问题,因为每个播放按钮都应存储视频的网址。
答案 13 :(得分:0)
当我们在modalpopup中使用嵌入视频时,我们如何暂停/停止YouTube iframe
$('#close_one').click(function (e) {
let link = document.querySelector('.divclass');// get iframe class
let link = document.querySelector('#divid');// get iframe id
let video_src = link.getAttribute('src');
$('.youtube-video').children('iframe').attr('src', ''); // set iframe parent div value null
$('.youtube-video').children('iframe').attr('src', video_src);// set iframe src again it works perfect
});
答案 14 :(得分:0)
找到iframe父元素或直接使用iframe的ID或类。
let temp_src = $('#sampleparent').find('iframe').attr('src');
$('#sampleparent').find('iframe').attr('src', '');
$('#sampleparent').find('iframe').attr('src', temp_src);