Question

I have managed to get my video autoplay inside my webview.apk, tested on my phone (4.1.2) and it works, while on my android mini pc (4.2) has to be clicked to start the playback...:(

Could WebChromeClient may be the reason? If it has something in common with chromium, which fails to autoplay as well, while the stock web browser plays nicely??

The javascript I use to get the autoplay working is the following:

<html>
  <head>

      <script>

function callback () {
    document.querySelector('video').play();
        }

       window.addEventListener("load", callback, false);


    </script>

  </head>



<body>
  <div id="video_post1" style="margin: -454px 0px 0px -3px;position: absolute;">
<video controls autoplay with="600" height="400">

  <source src="http://www.edmondvarga.com/demo/videos/trx.mp4" type="video/mp4">


</video>
  </div>

    </body>
    </html>

Maybe I would use another syntax to play it (like: document ready)?

Eclipse code, just for demonstration:

package tscolari.mobile_sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;

import android.media.MediaPlayer;


public class InfoSpotActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);

        WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebChromeClient(new WebChromeClient());

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);




        mainWebView.loadUrl("http://server.info-spot.net");
    }


    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
Était-ce utile?

La solution

I realize this is an old question but this answer might help someone down the road:

Android (and iOS both) disabled the autoplaytag in HTML5 video nodes in the Chrome or native browser. Playing a video requires user interaction (onClick or onMouseOver or something). However, one can autoplay videos in one's own WebView if necessary.

To autoplay videos in an Android Webview, post Android 4.4:

    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top