如何检测youtube视频何时播放完毕? [英] How to detect when a youtube video finishes playing?

查看:41
本文介绍了如何检测youtube视频何时播放完毕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个嵌入了大量 YouTube 视频的网站,客户希望在视频停止播放时显示一个弹出窗口.

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.

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

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).

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

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

推荐答案

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

This can be done through the youtube player 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>

这篇关于如何检测youtube视频何时播放完毕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆