I've been doing somewhat extensive experimenting with HTML5 Video for a while now. One issue that seems to keep arising is manipulating the currentTime property.
Here is a brief overview of my project and script:
I have a few arrays that store information (video file name, start time, duration, audio time) about a video sequence; the basic functionality is that a video clip starts playing, the next file starts loading, and when the currently playing video reaches its target duration, the loaded video starts playing, etc. At the moment I'm doing all this offline, so the actual downloading times are not an issue for me yet. So to simplify, my arrays would look something like this:
videoArray = ["clip01.webm", "clip05.webm", "clip03.webm"];
startArray = [0.5, 0, 1.5];
durationArray = [5, 2.1, 1.5];
The biggest issue I keep facing and solving is setting the start time of each video. Every time I get it to work, I seem to have to find a new solution a few weeks or months later. The functions in which this issue is happening are as follows: After initializing the first video, the script calls a time-update function (shortened code):
function timeUpdate() {
var clip = document.getElementById(currentVideoTagId);
clipTime = clip.currentTime;
drawClip(clip); // function that displays the clip in a canvas element
switchClip(); // function that checks for clip's target duration
setTimeout(timeUpdate,40);
}
This function, as you can see, draws the Video into a Canvas element, using SetTimeout. This is also where I keep checking if 1) the next clip to play has loaded and 2) if the video currently playing has reached its target duration (shortened code):
function switchClip() {
if(!nextIsLoading){
loadClip(nextSequenceIndex); // function that sets the video source, etc
$("#" + nextVideoTagId).bind('canplay', function() {
this.currentTime = sequence.starts[nextSequenceIndex];
});
nextIsLoading = true;
}
if(clipTime >= currentCut){
playClip(); // function that starts playing the video
nextIsLoading = false;
}
}
So using the jQuery bind that checks for 'canplay' was my latest solution and it has worked for a while. I had been doing all my experiments on a Mac for Chrome (newest version). I generated my sequence data with PHP out of a database. I recently made a switch to PC (using the newest version of Chrome again), as I've started to use Webmatrix to generate my sequence data for this new version of the project. The javaScript-part of the project is essentially the same, as far as I can tell, and everything works, apart from setting currentTime.
To be more exact: the video's currentTime still gets set, I've done a lot of console.logging to observe what's happening and where errors might occur, but, what is the most peculiar thing to me, happens here (shortened code):
function playClip(){
var newVideo = document.getElementById(currentVideoTagId);
console.log("currentTime before play: " + newVideo.currentTime);
newVideo.play();
console.log("currentTime after play: " + newVideo.currentTime);
}
The first log in this function always shows the currentTime I have set previously. After play() the currentTime is always back to 0.
I've tested quite a few things by now and I keep looking for answers on Google and here, on Stackoverflow (which is how came to this solution that has worked before in the first place), but I'm running out of solutions now.
Is there anyone who may be able to shed some light on this issue?
I hope this post provides enough information on the problem.