admin管理员组文章数量:1200985
Is there a way to display/see/return which video (so, which videoID) is playing at the moment?
This is the basic code of the Iframe API:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'pJ18QeQy0e8',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'onError': onError,
},
playerVars: {
'controls': 0,
'showinfo': 0,
'iv_load_policy': 3,
'wmode': "opaque",
},
});
}
after the first video has finished. This event will be called:
function onPlayerStateChange(event) {
if(event.data === 0) {
player.stopVideo();
player.loadVideoById(getId());
}
}
The getId() function will get another videoId to play..
I want to display in a DIV, which videoId is currently playing
Is there a way to display/see/return which video (so, which videoID) is playing at the moment?
This is the basic code of the Iframe API:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'pJ18QeQy0e8',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'onError': onError,
},
playerVars: {
'controls': 0,
'showinfo': 0,
'iv_load_policy': 3,
'wmode': "opaque",
},
});
}
after the first video has finished. This event will be called:
function onPlayerStateChange(event) {
if(event.data === 0) {
player.stopVideo();
player.loadVideoById(getId());
}
}
The getId() function will get another videoId to play..
I want to display in a DIV, which videoId is currently playing
Share Improve this question asked Nov 29, 2013 at 0:51 Marc SterMarc Ster 2,3166 gold badges36 silver badges64 bronze badges4 Answers
Reset to default 20player.getVideoData()['video_id']
OR
var video_data = player.getVideoData()
video_data['video_id']
Solution:
function onPlayerStateChange(event) {
if(event.data === 0) {
player.stopVideo();
player.loadVideoById(player.getVideoData()['video_id']);
}
}
You can use getVideoUrl to get the url and take the id part out of this url.
Using window.location.href...
You can paste this into the console on desktop and if you're on a youtube video it'll take the id out of the url, not elegant at all but it works for desktop
var url = window.location.href;
var isYTvid = url.indexOf("www.youtube.com/watch?v=");
if (isYTvid != -1) {
var id = url.slice(url.indexOf("=") + 1 , url.length);
console.log(id);
}
(Original created by Dal) - This version is edited to work with playlists
var url = window.location.href;
var isYTvid = url.indexOf("www.youtube.com/watch?v=");
if (isYTvid != -1) {
var id = url.split("&")[0].split("=")[1]
console.log(id);
}
本文标签: javascriptYoutube API See which video id is currently playingStack Overflow
版权声明:本文标题:javascript - Youtube API: See which video id is currently playing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738619026a2103081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论