我想知道,通过iframe嵌入YouTube视频会暴露某些事件,比如onStart或onStop,你可以指定一些回调吗?
答案 0 :(得分:8)
这是处理启动和停止事件的示例:
HTML文件(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):
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;
}
}
}
});
}
对于每种情况,您都可以设置处理程序。
更多信息:
答案 1 :(得分:1)
唯一使用的事件是:
1 - onStateChange
2 - onPlaybackQualityChange
3 - onError
4 - onApiChange
-Event Handlers:
1- onYouTubePlayerReady(playerid)