Call functions at a specific time during a HTML5 video
Include jQuery and VideoKeyframe
<script src="jquery.js"></script>
<script src="videoKeyframe.js"></script>
Init VideoKeyframe on your HTML5 video element
var videoKeyframe = new VideoKeyframe( element );
Wait until the video can play to add labels
function addLabels () {
videoKeyframe.addLabel(4, pause, {
name: 'label',
pause: true
});
}
$('video').on('canplay', addLabels);
adds a label in the timeline
- time:
int
time in the video where you want to put the label - callback:
func
function to be called at that point - options (optional)
returns label object
- label:
int
id of the label in the array orstring
label name
plays the video from a given label
- label:
int
id of the label in the array orstring
label name
Add a name to your label
videoKeyframe.addLabel( 2, myFunction, { name: 'myLabel' } );
default: null
Pause the video when you reach the label
videoKeyframe.addLabel( 2, myFunction, { pause: true } );
default: false
adds a label from the end of the timeline
videoKeyframe.addLabel( 2, myFunction, { fromEnd: true } );
default: false
var $video = $('video');
// init videoKeyframe
var videoKeyframe = new VideoKeyframe( $video);
$video.on('canplay', addLabels);
function addLabels () {
// add a label at 4sec that will call pause()
// name it label and pause the video once it reaches it
videoKeyframe.addLabel(4, pause, {
name: 'label',
pause: true
});
}
function pause () {
var label = videoKeyframe.getLabel(0);
console.log(label);
setTimeout(function(){
// resume video after 2sec
videoKeyframe.playFrom( 'label' );
}, 2000);
}