如何检测时,YouTube视频播放结束? [英] How to detect when a youtube video finishes playing?

查看:971
本文介绍了如何检测时,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:

<一个href="http://$c$c.google.com/apis/youtube/js_api_reference.html">http://$c$c.google.com/apis/youtube/js_api_reference.html

但我不能嵌入视频像他们的网页上提到的,因为视频都已经在网站上(千已手动添加粘贴嵌入code)。

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', {
              height: '390',
              width: '640',
              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天全站免登陆