HTML5 Canvas drawImage with video source not working on Android
Asked Answered
C

3

6

I am trying to use canvas's drawImage method with video source, but it's not working in Android 4.4.2, tested with Chrome browser.

Here's my code:

$(function() {

    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    var video = document.getElementById('video');

    var videoWidth; 
    var videoHeight;    

    var paused = false;

    canvas.addEventListener('click', function() {   
        if (paused) {
            video.play(); 
        } else {
            video.pause();
        }
        paused = !paused;
    });

    video.addEventListener("loadedmetadata", function() {
        videoWidth = this.videoWidth || this.width;
        videoHeight = this.videoHeight || this.height;
        canvas.width = videoWidth;
        canvas.height = videoHeight;
    }, false);

    video.addEventListener('play', function() {
        var $this = this; // cache
        (function loop() {
            if ( ! $this.paused && ! $this.ended ) {
                drawImage($this);
                requestAnimationFrame(loop);
                // setTimeout(loop, 1000 / 25); // drawing at 25 fps
            }
        })();
    }, 0);

    function drawImage(frame) {
        ctx.drawImage(frame, 0, 0);
        // ctx.clearRect ( 0 , 0 , canvas.width, canvas.height );
    }

});

Fiddle: http://jsfiddle.net/h1hjp0Lp/

Is there a way to make it works with Android and iOS devices as well?

Complexity answered 25/5, 2015 at 10:47 Comment(3)
Try to replace click event with touchstart, see if it works.Dilatant
@Allen, thanks, but it's not working.Complexity
reading this, it appears that there is a bug in Chrome mobile. I just checked previous demo's I've done with Video -> Canvas and they are no longer working. I will continue to investigate. (Android 5.1)Lechery
B
2

It seems to be an issue with that specific file format (mp4).

Substituting a webm file and it works fine.

Unfortunate, but that's been the way of <video> for a while (to really require webm/ogg and not just mp4, regardless of what browsers claim to support).

(I bet its a bug relating to anti-drm screenshot stuff? who knows)

for example use the source http://video.webmfiles.org/big-buck-bunny_trailer.webm and it will work: http://jsfiddle.net/h1hjp0Lp/15/

Burgenland answered 26/5, 2015 at 2:35 Comment(1)
On a device with android version lower then 5.0 the drawImage from a video still doesn't work...Montemontefiascone
P
0

I'm running Chrome 43.0.2357.93 on Android 5.0.1, and drawImage isn't working for mp4 or webm.

It looks like Chrome isn't properly capturing frame data from the video tag. When I call getImageData and inspect the resulting imageData object, the RGBA values are all set to 0.

Predestinarian answered 13/6, 2015 at 19:43 Comment(1)
I've found that this has recently stopped working on Chrome on Android a couple of months ago, it used to work. I've found that it works on Chrome Beta on Android. Here is my test: codepen.io/kus/full/aOqvWP I'm using Android 5.1.1 Chrome 43.0.2357.93Indicia
I
-1

For HUAWEI GRA-TL00 with Android 5.0.1, drawImage isn't working for mp4 or webm.

Refer to crestejs video Bitmap.

Related Links:

  1. Using Images

  2. Can I use: Canvas?

Indiaindiaman answered 10/9, 2017 at 13:7 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.