Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView NOT opening android default video player?

when I view this page on my android device' default web browser and click the first video, it triggers the default video player of my device. It loads and play.

However when I view the same link in my app, using a WebView, it does not open the default video player. What could be the problem?

I'm using the webview code in this link.

I also made the webview in full screen mode like what was said in the docs, is used this code to go fullscreen:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

EDIT: I'm now using the following code, but still not work, any ideas?

package com.example.Playmp4OnWebView;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

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

          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

          WebView webview = new WebView(this);
          setContentView(webview);

          WebSettings webSettings = webview.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setSupportZoom(false);
          webSettings.setPluginsEnabled(true);
          webSettings.setAllowFileAccess(true);

          webview.setWebViewClient(new WebViewClient(){

              public boolean shouldOverrideUrlLoading(WebView view, String url){
                   if(url.endsWith(".mp4")){
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i); //warning no error handling will cause force close if no media player on phone.
                        return true;
                   }
                   else return false; 
              }});

       //This will load the webpage that we want to see
        webview.loadUrl("http://www.broken-links.com/2010/07/30/encoding-video-for-android/");

     }
}
like image 222
Kris Avatar asked Jun 06 '11 01:06

Kris


2 Answers

You have to attach your own WebViewClient and override shouldOverrideUrlLoading() if you detect a URL with a supported video mimetype return true and then launch the default activity with the URL. Here is an untested sample.

mWebView.setWebViewClient(new WebViewClient(){

public boolean shouldOverrideUrlLoading(Webview view, String url){
     if(url.endsWith(".mp4") || url.endsWith("some other supported type")){
          Intent i = new Intent(Intent.ACTION_VIEW);
          i.setData(Uri.parse(url));
          startActivity(i); //warning no error handling will cause force close if no media player on phone.
          return true;
     }
     else return false; 
}});

hope this helps.

like image 89
Nathan Schwermann Avatar answered Nov 01 '22 23:11

Nathan Schwermann


If you are getting a html5 video tag in the WebView then the above code (using WebViewClient) will not be called, instead you will have to handle it with WebChromeClient as shown in the bellow link

How to play a video in a webview with android?

I have tried it out and it worked well :)

the above code (using WebViewClient) is useful only when we have to handle a redirect request to another page via user click on anchor tag or through any hyperlink in the WebView

like image 28
Big-M Avatar answered Nov 01 '22 23:11

Big-M