2016-07-28 67 views

回答

5

您可以使用Youtube Iframe-API循环播放视频部分。

将这个标签在你的HTML页面:

<!-- 1. The <iframe> (and video player) will replace this <div> tag. --> 
<div id="player"></div> 

负载的Youtube的Iframe-API

// 2. This code loads the IFrame Player API code asynchronously. 
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 section = { 
    start: 30, 
    end: 33 
}; 

// 3. This function creates an <iframe> (and YouTube player) 
// after the API code downloads. 
var player; 
function onYouTubeIframeAPIReady() { 
    player = new YT.Player(
    'player', 
    { 
     height: '360', 
     width: '640', 
     videoId: 'zeI-JD6RO0k', 
     events: { 
     'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
     } 
    } 
); 
} 

function onPlayerReady(event) { 
    player.seekTo(section.start); 
    player.playVideo(); 
} 

function onPlayerStateChange(event) { 
    if (event.data == YT.PlayerState.PLAYING) { 
    var duration = section.end - section.start; 
    setTimeout(restartVideoSection, duration * 1000); 
    } 
} 

function restartVideoSection() { 
    player.seekTo(section.start); 
} 

See this example in action