rand
rand

Reputation: 322

Video not playing on android webview

I am working with an Android and PhoneGap application and am using the HTML5 video tag to play videos on my web page. When I play the video is not visible and video is not playing itself. How can I play a HTML5 video on Android?

Code for the same given below

<!DOCTYPE HTML>
<html>
    <head>
    <script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>
        <meta http-equiv="content-type" content="text/html; charset=">
        <title></title>
    </head>
    <body >


        <video id="video" autobuffer height="240" width="360" onclick="this.play();>
<source src="test.mp4">
<source src="test.mp4" type="video/webm">
<source src="test.mp4" type="video/ogg">
</video>

        <div id="msg"></div>

<script type="text/javascript">

</script>

    </body>
</html>

and the activity class onCreate method-->>

public void onCreate(Bundle savedInstanceState) {            
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView webView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); 

        webView.getSettings().setJavaScriptEnabled(true);

        webSettings.setBuiltInZoomControls(true);
        webSettings.setPluginState(PluginState.ON);

        webView.getSettings().setPluginsEnabled(true);
        webSettings.setAllowFileAccess(true);

        webView.loadUrl("file:///android_asset/www/html5videoEvents.html");      
}

Upvotes: 5

Views: 15161

Answers (2)

Sreekanth
Sreekanth

Reputation: 407

Add these lines before loading HTML content to your WebView.

wv.setWebChromeClient(new WebChromeClient() {
});

From the documentation:

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient. For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required, getVideoLoadingProgressView() is optional.

Upvotes: 4

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Try this.

MainActivity.java

public class MainActivity extends Activity {

    WebView webPage;
    Button next;
    String rootDir = "file://" + Environment.getExternalStorageDirectory()
            + "/iR-unzip/testbook/";
    WebChromeClient webChromeClient = new WebChromeClient();
    WebViewClient webViewClient = new WebViewClient();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webPage = (WebView) findViewById(R.id.webview);
        webPage.setWebChromeClient(webChromeClient);
        webPage.setWebViewClient(new MyWebViewClient());
        webPage.getSettings().setJavaScriptEnabled(true);
        webPage.getSettings().setPluginsEnabled(true);
        //webPage.getSettings().setPluginState(PluginState.ON);
        webPage.getSettings().setLoadWithOverviewMode(true);
        webPage.getSettings().setUseWideViewPort(true);

        webPage.loadUrl(rootDir + "/" + "chapter_1.html");
        next = (Button) findViewById(R.id.page_changer);
        next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                webPage.loadUrl(rootDir + "/" + "chapter_2.html");
            }
        });

    }

    public class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // TODO Auto-generated method stub
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    VideoView video = (VideoView) frame.getFocusedChild();
                    frame.removeView(video);
                    video.start();
                }
            }

        }
    }

    public class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

Upvotes: 1

Related Questions