如何检测时,YouTube视频播放结束?视频播放、结束、YouTube

2023-09-08 10:57:34 作者:心口的朱砂痣

我工作的一个网站,有大量的嵌入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://$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>