It is possible, but from my own testing, the Youtube API is rather buggy, and you'll need to go through some trial and error before getting things to work. The below example starts the first video at 5 seconds in, plays it for 2 seconds before playing another video from 5 seconds in and stops at 10 seconds:
Code:
<script type="text/javascript">
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
playerVars: {
//'autoplay': 1,
//'start': 10,
//'end': 15,
'controls': 1,
'autohide': 1,
//'wmode': 'opaque',
'showinfo': 0,
'loop': 0,
'playlist': ['MVqAA9_7gR4', 'tp5hgjD_Y5g']
},
videoId: 'MVqAA9_7gR4',
events: {
'onReady': onPlayerReady,
'onStateChange': function(e){
if (e.data == 0){
//if current video has finished playing
}
}
}
});
}
function onPlayerReady(event) {
//event.target();
$('#text').fadeIn(400);
player.seekTo(5, false) // start first video at 5 seconds in
player.playVideo()
setTimeout(function(){
player.stopVideo()
player.loadVideoById({
'videoId': 'tp5hgjD_Y5g',
'startSeconds': 5, // play second video 5 seconds in
'endSeconds': 10 // stop second video 10 seconds in
})
}, 2000) // stop first video after 2 seconds
}
$(window).scroll(function() {
var hT = $('.m-video').height(),
wS = $(this).scrollTop();
if (wS > hT) {
player.pauseVideo();
}
else {
player.playVideo();
}
});
</script>
Sorry the indentation seems to be all messed up. All the logic happens inside onPlayerReady(). I find that if you specified the start/end times of the initial video using the "start" and "end" settings, it doesn't work properly (hence I commented them out). Also, the "videoId" setting if defined causes the "playlist" option to no longer be honored, so I also had to comment that out. In other words, the API doesn't seem to behave the way it's described to, so you'll need to play around with other approaches to get the desired outcome.
Bookmarks