admin管理员组文章数量:1194135
I've managed to start the video and end the video at the times I need, but is there any way to loop this? The loop option doesn't seem to be doing much.
Fiddle: /
Code:
<div id="ytplayer"></div>
<script>
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '360',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
autoplay: 1, // Auto-play the video on load
controls: 0, // Show pause/play buttons in player
showinfo: 0, // Hide the video title
modestbranding: 1, // Hide the Youtube Logo
fs: 1, // Hide the full screen button
cc_load_policy: 0, // Hide closed captions
iv_load_policy: 3, // Hide the Video Annotations
start: 36,
end: 45,
loop: 1, // Run the video in a loop
autohide: 0 // Hide video controls when playing
},
});
}
</script>
I've managed to start the video and end the video at the times I need, but is there any way to loop this? The loop option doesn't seem to be doing much.
Fiddle: https://jsfiddle.net/u7nkz292/
Code:
<div id="ytplayer"></div>
<script>
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '360',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
autoplay: 1, // Auto-play the video on load
controls: 0, // Show pause/play buttons in player
showinfo: 0, // Hide the video title
modestbranding: 1, // Hide the Youtube Logo
fs: 1, // Hide the full screen button
cc_load_policy: 0, // Hide closed captions
iv_load_policy: 3, // Hide the Video Annotations
start: 36,
end: 45,
loop: 1, // Run the video in a loop
autohide: 0 // Hide video controls when playing
},
});
}
</script>
Share
Improve this question
edited Oct 7, 2017 at 3:06
Bertrand Martel
45.4k17 gold badges149 silver badges166 bronze badges
asked Feb 16, 2017 at 10:42
NickNick
2,5515 gold badges36 silver badges71 bronze badges
1
- Check this link – kaido Commented Feb 16, 2017 at 10:51
2 Answers
Reset to default 16You can implement onStateChange
callback & load the video with the same startSeconds
& endSeconds
parameter with loadVideoById
:
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var videoId = 'M7lc1UVf-VE';
var startSeconds = 36;
var endSeconds = 45;
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
var playerConfig = {
height: '360',
width: '640',
videoId: videoId,
playerVars: {
autoplay: 1, // Auto-play the video on load
controls: 0, // Show pause/play buttons in player
showinfo: 0, // Hide the video title
modestbranding: 1, // Hide the Youtube Logo
fs: 1, // Hide the full screen button
cc_load_policy: 0, // Hide closed captions
iv_load_policy: 3, // Hide the Video Annotations
start: startSeconds,
end: endSeconds,
autohide: 0, // Hide video controls when playing
},
events: {
'onStateChange': onStateChange
}
};
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', playerConfig);
}
function onStateChange(state) {
if (state.data === YT.PlayerState.ENDED) {
player.loadVideoById({
videoId: videoId,
startSeconds: startSeconds,
endSeconds: endSeconds
});
}
}
Here is a Fiddle
Iterating on Bertrand Martel's great answer above:
If you're looping on the same video, I've found you can minimize the delay in between loops by calling player.seekTo(startSeconds)
instead of player.loadVideoById(...)
. Here is a fiddle comparing the performance of both approaches.
本文标签: javascriptYouTube APILoop video between set start and end timesStack Overflow
版权声明:本文标题:javascript - YouTube API - Loop video between set start and end times - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738498805a2090146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论