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

编程入门 行业动态 更新时间:2024-10-20 09:24:21
本文介绍了如何检测youtube视频何时播放完毕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在处理一个嵌入了大量 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/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/7Gznb/

工作示例:

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

    <script src="http://www.youtube/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 17:12:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/943686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视频   youtube

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!