admin管理员组文章数量:1327945
Is there an "onload" of the video for an embeded youtube iframe?
I'd like to start my script only after the music video i have selected starts.
I have stuck an onload event onto the iframe the youtube video is delivered in, but that does not correspond to the loading (buffering has finished ready to play) of the actual video. It only corresponds to when the video player has been loaded in the page.
In other words, is there any way to detect with javascript when a youtube video starts playing?
Is there an "onload" of the video for an embeded youtube iframe?
I'd like to start my script only after the music video i have selected starts.
I have stuck an onload event onto the iframe the youtube video is delivered in, but that does not correspond to the loading (buffering has finished ready to play) of the actual video. It only corresponds to when the video player has been loaded in the page.
In other words, is there any way to detect with javascript when a youtube video starts playing?
Share Improve this question edited Feb 5, 2016 at 3:21 Ulad Kasach asked Feb 4, 2016 at 4:09 Ulad KasachUlad Kasach 12.9k13 gold badges69 silver badges89 bronze badges 2-
Not sure why you got downvoted... but I do think a little more detail would help. Can you tell us what you've tried? Technically you can add an event listener for the
load
event of any element. That event will fire when the element and all of it's children have finished loading. – vastlysuperiorman Commented Feb 4, 2016 at 4:21 - @vastlysuperiorman I've updated the question with some more background information and added a more to the point question. – Ulad Kasach Commented Feb 5, 2016 at 3:23
1 Answer
Reset to default 4Everything you're looking for can be found in the Youtube Iframe API reference.
I'll paste the relevant code here as well, but be aware that I only modified one line to trigger an alert onReady. The rest of the code es from the aforementioned reference page.
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube./iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
alert("Video Ready!");
event.target.playVideo(); // You can omit this to prevent the video starting as soon as it loads.
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
本文标签: Is there any way to detect with javascript when a youtube video starts playingStack Overflow
版权声明:本文标题:Is there any way to detect with javascript when a youtube video starts playing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742222593a2435545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论