15

I want to auto-start android html5 video using android 4 ice cream sandwich browser. I tried many java-script functions and autobuffer autoplay tags of html5 video. But nothing worked. I start android chrome client in webview via android app and that client should be able to auto-start video. When click the play button video plays but not auto play.

Is it restricted in android? Other thing to notice is that no call back methods are called in chromeClient even when we click the play button & video is playing & completed.

I have googled & found no positive result on this issue on Android 4.

M P Mathugama
  • 1,258
  • 1
  • 18
  • 30

2 Answers2

23

It seems that Android 4+ changed the requirements for the play() method to require user interaction. If you trigger play() from within a user event handler (eg. touchstart or mousedown), then you can play the video as long as you run it inside the same event loop.

This means that you shouldn't use async triggers to call play(), but rather call play inside the same event handler without setTimeout() and such, so stuff like time-delayed play is out of the question.

One way is to use the same trick on Android 4 as in iOS – use the first user interaction event to play() and pause() the video. This will enable the video for manipulation later, since you played it during a user initiated action. After you've successfully primed the video, you can call play methods at any time later, regardless of whether the call was made inside the event handler loop or not.

EDIT: Here's a sample code that works on HTC and Samsung, but not Galaxy Nexus 4.1 (requires user interaction to play):

var myVideo = document.getElementById('myvideo');

myVideo.addEventListener('canplay', function() {
  myVideo.play();
});

myVideo.load();
myVideo.play();
Klemen Slavič
  • 19,181
  • 3
  • 31
  • 42
  • Thanks Krof. Does that mean all the callback methods such as onShowCustomView, onCompletion, onPrepared etc in WebCromeClient are not being called for the various events of the video while it is playing? One more thing What does it mean by prime the video. Is there any tutorial or example on what your mentioning? – M P Mathugama Aug 06 '12 at 12:56
  • 1
    _Primed_ in this case means video already played, which implies that the video play was triggered by the user and is therefore available for playing. Not sure what you mean by "WebChromeClient" though. – Klemen Slavič Aug 06 '12 at 13:13
  • Krof, This will not work on Samsung S3 & Tab 2.0 devices as you mentioned in Nexus 4.1, user interaction is needed. – M P Mathugama Aug 15 '12 at 12:34
  • Yeah, it's still an incomplete solution, sometimes Samsung S3 requires you to kill the browser and then it just miraculously works. It's the worst and slowest browser of all the Androids. – Klemen Slavič Aug 16 '12 at 09:32
  • 1
    On a Nexus 10/Android 4.2, it is possible to [trigger playback across a setTimeout](https://gist.github.com/BCdmlap/5339273) but only a single one. – dml Apr 08 '13 at 19:55
  • Solved my problem on the truly hateful Galaxy Tab 3. Thanks. – SpaceBeers Dec 02 '15 at 11:01
1

Android actually has an API for this! The method is setMediaPlaybackRequiresUserGesture(). I found it after a lot of digging into video autoplay and a lot of attempted hacks from SO. Here's an example from blair vanderhoof:

package com.example.myProject;

import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebSettings;

public class myProject extends CordovaActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html");

        WebSettings ws = super.appView.getSettings();
        ws.setMediaPlaybackRequiresUserGesture(false);
    }
}

works on Android 4.4.4

brendan
  • 727
  • 7
  • 9