停止嵌入式 YouTube iframe?

IT技术 javascript html iframe youtube-api
2021-02-01 09:02:40

我正在使用 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' 
6个回答

不幸的是,这些 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":""}', '*')
});
截至 2018 年 5 月,这仍然有效。修复了 js 代码中的一个装饰性拼写错误。
2021-03-25 09:02:40
2020 年 5 月 25 日,此解决方案仍然有效,添加?enablejsapi=1iframeElement.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*')(注意我正在使用 pauseVideo,因为即使视频未启动,stopVideo 也会隐藏标题)。
2021-03-26 09:02:40
我想强调的是,您的嵌入 url 必须具有enablejsapi=1,否则这是行不通的。此处的详细信息developers.google.com/youtube/player_parameters#enablejsapi 我花了一些时间才意识到为什么这个答案不起作用。
2021-03-27 09:02:40
@MarcoFaustinell 我找到了命令,它是:取消静音。它区分大小写,因此请注意大小写。
2021-04-03 09:02:40
我知道这篇文章很旧,但我只想说这个解决方案到 2020 年 7 月仍然有效。
2021-04-06 09:02:40

如果有人仍在寻找答案,我已经这样解决了:

$("#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);
      });
    });

应该做的伎俩。

哈!拿那个 YouTube!聪明人得分!
2021-03-15 09:02:40
我认为这是动态加载 iframe 时的最佳选择。
2021-03-21 09:02:40
好提示,谢谢。我并入:visible其中,而不是重置页面上的所有内容,而是重置当前的页面。翻阅选项卡或图库时,这意味着新选项卡将显示已加载的视频。
2021-03-21 09:02:40
也许 YouTube 端发生了一些变化,但对我来说,这只是重新启动视频而不是停止它(编辑:没关系,其他地方的 javascript 正在向 url 添加“&autoplay=1”)
2021-03-24 09:02:40
是的!谢谢。这个选项在 2019 年仍然有效。此外,它不依赖于 YouTube API。
2021-04-02 09:02:40

您可能需要查看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();
  });
});
@Danpe 似乎可以在这里找到一个完整的工作示例:stackoverflow.com/questions/7443578/...
2021-03-17 09:02:40
stackoverflow.com/questions/9310112/...包含有关跨站点保护错误的信息。(通常是 chrome 中的一个大问题。)至于第二个,您需要更改.myVideoClass附加到您的嵌入的类。IE.yvideo
2021-03-21 09:02:40
我越来越: Uncaught TypeError: Object [object Object] has no method 'stopVideo'
2021-03-28 09:02:40
但是我需要为每个玩家创建一个对象?stopVideo() 会在没有任何事先设置的情况下工作吗?
2021-04-03 09:02:40
如果您添加enablejsapi=1到嵌入中,您应该能够引用 API。需要为每个视频添加它。
2021-04-07 09:02:40

这是一个codepen,它对我有用

我正在寻找在 iframe 中嵌入 YT 视频的最简单的解决方案,我觉得就是这样。

我需要的是让视频出现在模式窗口中并在关闭时停止播放

这是代码:(来自: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&放置它的位置以及在 next 属性之前使用“&”以允许暂停继续工作。

因此,与autoplay=1中,1只意味着它应该不会暂停(自动播放),你说如果你把&后,将不会自动播放和暂停视频?
2021-03-17 09:02:40
在没有来自 youtube 的所有这些 JS 和 API 的情况下停止播放器的最简单方法,谢谢!
2021-03-24 09:02:40
刚刚也试过了,好像不行。什么可能是新的变化。哈哈。
2021-03-28 09:02:40
我认为它不再起作用,至少在 Chrome 96 上,我也检查了 Firefox 89.0.2 并且它起作用了,但在 Chrome 中不起作用:(
2021-04-06 09:02:40

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.
    }
}
这是正在寻找的接近解决方案。现在在我尝试重置之前检测它是否正在播放。它会导致视频闪烁。
2021-03-25 09:02:40