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;
}
}
}