• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Enabling HTML5 video playback in android WebView?

Vedtam

Lurker
Jun 19, 2013
9
0
Hi,

I am building a simple webview application which is now displaying a website filled with short video clips, using the HTML5 video player. Everything runs ok in the default android web browser but webview wont't play any of the video clips.


Html code used to play the video clips:

[HIGH]<video poster preload="true" controls autoplay with="500" height="200">
<source src="http://www.edmondvarga.com/demo/videos/video.mp4" type="video/mp4">
</video>[/HIGH]




Main Activity.java :

[HIGH]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;

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.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;
}
}
}
[/HIGH]

How could I enable video playback inside webview?


Best regards,
vedtam
 
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;
}
}
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones