5

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/");

     }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Kris
  • 3,709
  • 15
  • 50
  • 66

2 Answers2

3

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.

Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • Hi @schwiz, thanks a lot for your response, I updated the question with my updated code and your suggestion code. But it still doesn't trigger the default media player of my device. Any ideas? – Kris Jun 06 '11 at 02:47
  • @Kris well if its loading in the same webview instead of launching an activity or forceclosing then the URL must not end with mp4 I would set a breakpoint in shouldOverrideUrlLoading and see what's happening in there. – Nathan Schwermann Jun 06 '11 at 04:56
  • when I click the vid, nothing happnes, it does not load or launch any activity or force closing. Actually, the first vid im clicking does not use href, it uses html5 – Kris Jun 06 '11 at 05:33
  • Well anyway, I'm now using the android default video view to play those videos I have from the sdcard... Thanks :) – Kris Jun 10 '11 at 03:15
  • 1
    @Kris best advice I can give is to put a break point or print to the logs the URL and see what you are clicking on and think of a way to test for it. – Nathan Schwermann Jun 10 '11 at 22:33
2

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

Community
  • 1
  • 1
Big-M
  • 21
  • 2