我有一个播放Youtube播放列表的无铬Youtube iframe api播放器。所有的工作都很好,但我想在一个div中显示当前播放视频的标题下面的播放器,但我不知道如何做到这一点,任何帮助将非常感激。
我试过stackoverflow的其他例子,但它们是内联播放列表,是不切实际的对于一个有100多个视频的youtube播放列表,我的剧本知识非常有限,我从来没有让他们工作而谷歌API网站似乎并没有涵盖我所寻找的。我尝试了jwplayer,它可以获得标题,但shuffle功能很差,一遍又一遍地播放相同的视频有时一个视频多次播放。我找到了一个可以解决shuffle问题的脚本,但是我丢失了get标题函数。我决定继续使用youtube,希望有人能帮助我。
所以再一次,我真的很感谢任何帮助与这个谢谢。这是我的
<!DOCTYPE html>
<html>
<body>
<div id="player"></div>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
playerVars : { 'rel' : 0, 'showinfo' :0, 'autoplay': 1, 'controls': 0 },
events: {
'onReady': onPlayerReady
}
});
}
var getRandom = function(min, max) {
return Math.random() * (max - min) + min;
`enter code here`};
var playRandomTrack = function() {
num = getRandom(0, 99);
player.playVideoAt(num);
};
function onPlayerReady(event) {
player.loadPlaylist({'listType': 'playlist', 'list': 'PLmc_AnfxCB6t6Lwwgx3x5OxfNOWwHluU-','index': '99','startSeconds': '0','suggestedQuality': 'hd720'});
player.setShuffle({'shufflePlaylist' : 1});
}
</script>
</body>
</html>
为第一个视频添加onPlayerReady():
var intId = setInterval( function() {
// Check if player is initialized and the video is loaded
if ( player ) {
// States: 1: playing, 2: paused, 5: stopped
if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
// Set the innerText of div with id="title" to player title
document.getElementById( "title" ).innerText = player.getVideoData().title;
clearInterval( intId );
}
}
}, 100 );
并添加到playRandomTrack():
// Delay the loop because otherwise player.getPlayerState() still returns 1 because the previous video is not unloaded yet
setTimeout( function() {
var intId = setInterval( function() {
if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
document.getElementById( "title" ).innerText = player.getVideoData().title;
clearInterval( intId );
}
}, 100 );
}, 100 );
所以你的整个代码变成了:
<!DOCTYPE html>
<html>
<body>
<div id="player"></div>
<div id="title"></div>
<script>
var tag = document.createElement( 'script' );
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName( 'script' )[0];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player( 'player', {
height: '390',
width: '640',
playerVars: {
'rel': 0,
'showinfo': 0,
'autoplay': 1,
'controls': 0
},
events: {
'onReady': onPlayerReady
}
} );
}
var getRandom = function( min, max ) {
return Math.random() * ( max - min ) + min;
};
var playRandomTrack = function() {
num = getRandom( 0, 99 );
player.playVideoAt( num );
setTimeout( function() {
var intId = setInterval( function() {
if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
document.getElementById( "title" ).innerText = player.getVideoData().title;
clearInterval( intId );
}
}, 100 );
}, 100 );
};
function onPlayerReady( event ) {
player.loadPlaylist( {
'listType': 'playlist',
'list': 'PLmc_AnfxCB6t6Lwwgx3x5OxfNOWwHluU-',
'index': '99',
'startSeconds': '0',
'suggestedQuality': 'hd720'
} );
player.setShuffle( {
'shufflePlaylist': 1
} );
var intId = setInterval( function() {
if ( player ) {
if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
document.getElementById( "title" ).innerText = player.getVideoData().title;
clearInterval( intId );
}
}
}, 100 );
}
</script>
</body>
</html>
我发现上面的实现不适合我,所以我想出了我自己的版本,我认为它更优雅。
var tag = document.createElement( 'script' );
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName( 'script' )[0];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player( 'player', {
height: '390',
width: '640',
playerVars: {
'rel': 0,
'showinfo': 0,
'autoplay': 1,
'loop': 1,
'autohide': 1,
'color': 'white',
'theme': 'light',
'controls': 1
},
events: {
'onReady': onPlayerReady
}
} );
}
var num = (1 + Math.floor(Math.random() * 10));
function onPlayerReady( event ) {
/*player.addEventListener('onStateChange', function(e) {
console.log('State is:', e.data);
});*/
player.addEventListener('onStateChange', 'onPlayerStateChange');
player.loadPlaylist( {
'listType': 'playlist',
'list': 'PLbfrQv4OD4BDMDrxuFv3uxwZeOKzkTkut',
'index': num,
'startSeconds': '0',
'suggestedQuality': 'hd720'
} );
player.setShuffle( {
'shufflePlaylist': 1
} );
}
function onPlayerStateChange( event ) {
console.log(player.getPlayerState())
if (player.getPlayerState() == 1) {
document.getElementById( "title" ).innerText = player.getVideoData().title;
}
}