I need to play videos that have a transparent background on mobile devices using HTML5. On the Windows Phone, I can capture the video tag's current frame and show that in the canvas. But that doesn't work on Android and iOS devices (I think for security reasons?)
My solution was to split up the .flv using FFMPEG and then stitch those frames together into large images, like sprite sheets.
The Problem is that the animation 'hangs' when I switch over to a new frame sheet. I've only checked this visually and through the console (by logging when I change the current sprite sheet row.) I've tested this by seeing how it hangs when I change the sprite sheet, and how it doesn't hang when I just loop the same sheet over and over.
I pre-load all of the images before hand:
var frameImages = [];
for(var i = 0; i < 35; i++)
{
frameImages.push(new Image());
frameImages[i].src = 'frame' + i + '.png';
console.log(frameImages[i].src);
frameImages[i].onload = function()
{
// Notify us that it's been loaded.
console.log("Image loaded");
}
}
And then play it like so:
processFrame = function()
{
outputCanvas.width = outputCanvas.width;
output.drawImage(frameImages[currentFrame], (curCol*153), (curRow*448), 153, 448, 0, 0, 153, 448);
curCol += 1;
if(curCol >= maxCol)
{
curCol = 0;
curRow += 1;
if(curRow >= maxRow)
{
curRow = 0;
currentFrame++;
}
}
}
}
var mozstart = window.mozAnimationStartTime;
step = function(timestamp) {
var diff = (new Date().getTime() - start) - time;
if(diff >= frameDelay)
{
processFrame();
time += frameDelay;
}
}
I've tried this in Chrome v 23.0.1271.97 m on a Win 7 machine and on a Nexus 7 with Chrome.
See it in action here:
http://savedbythecode.com/spokes/mozanimation.php - This is using mozAnimationStartTime
and http://savedbythecode.com/spokes/newplayer.php - This is using regular JS timers that are adjusted each step (from http://www.sitepoint.com/creating-accurate-timers-in-javascript/)
Any ideas? Was the problem clear enough?
Thanks, Kevin