我正在使用iframe的YouTube播放器API参考 简单的代码
<div id="player"></div>
<script>
//Load player api asynchronously.
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var done = false;
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '327',
width: '530',
videoId: '_XaWYzsY920',
playerVars: { 'autoplay': 1,'playlist':['Hzgzim5m7oU', 'QvZIYIVNGZ0']},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(evt) {
evt.target.playVideo();
}
function onPlayerStateChange(evt) {
console.log(evt.data);
console.log(evt.target.getVideoUrl());
}
</script>
我想与evt.data == 5一起工作 但它永远不会被解雇......
有办法吗?
答案 0 :(得分:4)
在onPlayerStateChange
内使用evt.target.getVideoUrl()
获取视频网址,然后使用正则表达式提取视频ID:
function onPlayerStateChange(evt) {
if (evt.data == YT.PlayerState.PLAYING) {
var url = evt.target.getVideoUrl();
// "http://www.youtube.com/watch?v=gzDS-Kfd5XQ&feature=..."
var match = url.match(/[?&]v=([^&]+)/);
// ["?v=gzDS-Kfd5XQ", "gzDS-Kfd5XQ"]
var videoId = match[1];
}
}
答案 1 :(得分:0)
var videoId = evt.target.getVideoData().video_id