本文介绍了Youtube 嵌入式视频开始/停止事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,通过 iframe 嵌入 youtube 视频是否会暴露某些事件,例如 onStart 或 onStop,您可以在其中指定一些回调?

I was wondering, does embedding a youtube video via iframe expose certain events, like onStart or onStop, where you can specify some callback?

推荐答案

这是一个处理启动和停止事件的例子:

This an example to handle start and stop events:

HTML 文件(index.html):

HTML file (index.html):

<!DOCTYPE html>
<html>
    <head>
        <title>Stackoverflow</title>
        <script type="text/javascript" src="http://www.youtube.com/player_api"> </script>
        <script type="text/javascript" src="sof.js"> </script>
    </head>
    <body>
        <div id="player"></div>
    </body>
</html>

还有 JavaScript (sof.js):

And the JavaScript (sof.js):

var player;
// This function creates an <iframe> (and YouTube player)
// after the API code downloads.
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'u1zgFlCw8Aw',
        events: {
            'onStateChange': function (event) {
                switch (event.data) {
                    case -1:
                        console.log ('unstarted');
                        break;
                    case 0:
                        console.log ('ended');
                        break;
                    case 1:
                        console.log ('playing');
                        break;
                    case 2:
                        console.log ('paused');
                        break;
                    case 3:
                        console.log ('buffering');
                        break;
                    case 5:
                        console.log ('video cued');
                        break;
                }
            }
        }
    });
}

您可以为每种情况设置一个处理程序.

For each case you can set an handler.

更多信息:

  1. YT 播放器入门
  2. YT Javascript API 事件

这篇关于Youtube 嵌入式视频开始/停止事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:20