如何检测 YouTube 视频何时播放完毕?

发布于 2024-12-11 16:07:27 字数 375 浏览 1 评论 0原文

我正在一个有大量嵌入式 YouTube 视频的网站上工作,客户希望在视频停止播放时显示一个弹出窗口。

我查看了 youtube api,似乎有一种方法可以检测视频何时结束:

http ://code.google.com/apis/youtube/js_api_reference.html

但我无法像他们在该页面上提到的那样嵌入视频,因为这些视频都已在网站上(已手动添加了数千个)经过粘贴嵌入代码)。

有没有办法在不更改任何现有视频的情况下检测这些视频的结尾(使用 javascript)?

I am working on a site that has a ton of embedded youtube videos, the client wants to show a popup whenever a video stops splaying.

I looked at the youtube api and there seems to be a way to detect when a video ends:

http://code.google.com/apis/youtube/js_api_reference.html

but I can't embed the videos like they mentioned on that page since the videos are all already on the site (thousands that have been added manually by pasting embed code).

Is there a way to detect the ending of these videos without changing any of the existing videos (using javascript)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-12-18 16:07:27

这可以通过 youtube 播放器 API 来完成:

http://jsfiddle.net/7Gznb/

工作示例:

    <div id="player"></div>

    <script src="http://www.youtube.com/player_api"></script>

    <script>

        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              width: '640',
              height: '390',
              videoId: '0Bmhjf0rKe8',
              events: {
                onReady: onPlayerReady,
                onStateChange: onPlayerStateChange
              }
            });
        }

        // autoplay video
        function onPlayerReady(event) {
            event.target.playVideo();
        }

        // when video ends
        function onPlayerStateChange(event) {        
            if(event.data === 0) {          
                alert('done');
            }
        }

    </script>

This can be done through the youtube player API:

http://jsfiddle.net/7Gznb/

Working example:

    <div id="player"></div>

    <script src="http://www.youtube.com/player_api"></script>

    <script>

        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              width: '640',
              height: '390',
              videoId: '0Bmhjf0rKe8',
              events: {
                onReady: onPlayerReady,
                onStateChange: onPlayerStateChange
              }
            });
        }

        // autoplay video
        function onPlayerReady(event) {
            event.target.playVideo();
        }

        // when video ends
        function onPlayerStateChange(event) {        
            if(event.data === 0) {          
                alert('done');
            }
        }

    </script>
梦言归人 2024-12-18 16:07:27

您可能想要做的是在所有页面上包含一个执行以下操作的脚本...
1. 找到 youtube-iframe :按标题的宽度和高度搜索它,或在其源中查找 www.youtube.com。你可以通过...来做到这一点
- 通过 for-in 循环遍历 window.frames,然后通过属性过滤

  1. 在当前页面的 iframe 中注入 jscript 添加 onYoutubePlayerReady 必须包含函数
    http://shazwazza.com/post/Injecting-JavaScript-into-other -frames.aspx

  2. 添加事件监听器等..

希望这有帮助

What you may want to do is include a script on all pages that does the following ...
1. find the youtube-iframe : searching for it by width and height by title or by finding www.youtube.com in its source. You can do that by ...
- looping through the window.frames by a for-in loop and then filter out by the properties

  1. inject jscript in the iframe of the current page adding the onYoutubePlayerReady must-include-function
    http://shazwazza.com/post/Injecting-JavaScript-into-other-frames.aspx

  2. Add the event listeners etc..

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文