Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling HTML5 video playback in android WebView?

Tags:

html

webview

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.

The Html code used to play the video clips is as follows:

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

Main Activity.java :

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

How could I enable video playback inside webview?

like image 551
Edmond Tamas Avatar asked Jun 23 '13 10:06

Edmond Tamas


People also ask

How do I open an HTML file in WebView?

The WebView method to load our file takes a URI , so we need to access the HTML file using that URI . Since we stored it in the assets folder, we can access it using file:///android_asset/{file_name} . Now let's load that file in our MainActivity .

How can I make WebView keep a video or audio playing in the background?

Solution. After searching a lot, I found this thread. I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened.

What browser is used in WebView?

On Android 7.0, 8.0 and 9.0, Google Chrome handles the embedded browsing functions. On other Android OS versions, web app performance may suffer without WebView enabled.

What is WebView HTML?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout.


2 Answers

I know from a previous project we did that you need to use the WebChromeClient to get HTML5 video to play. (And this also gives you hardware accelerated support too - providing you also set the flags on your activity).

Use:

        mainWebView.setWebChromeClient(new WebChromeClient());

Put that before you set the setWebViewClient. You can override the WebChromeClient to intercept any events you need to handle.

And in your AndroidManifest.xml within your activity definition, add:

android:hardwareAccelerated="true"

The following quote is from this SDK Page (scroll down to HTML5 Video support)

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient.

like image 182
peterept Avatar answered Oct 03 '22 22:10

peterept


As far as I know android webview has Chromium engine and it does not support HTML 5 videos (H.264) in fullscreen mode. You can try playing video but video goes out of the screen and its not consistence across all the devices. If webview is the main part of the app then its better to switch to mozilla geckoview but it will add 30 to 40 mb to your apk.

like image 26
ashwath hegde Avatar answered Oct 03 '22 23:10

ashwath hegde