How to open a mp4 video in Android webview?

Issue

In my application, there is a list of some videos. I used listView for that. When a user clicks to a list element, webViewActivity is starting and I just want to open a "mp4" file in this webView. I came up with this solution:

WebViewActivity Class:

public class WebviewActivity extends Activity{
    private  WebView webview;
    private String url;
    @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_webview);

          webview  (WebView) findViewById(R.id.webview);
          webview.setWebViewClient(new WebViewClient());

          webview.getSettings().setJavaScriptEnabled(true); 
          webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
          webview.getSettings().setSupportMultipleWindows(true);
          webview.getSettings().setSupportZoom(true);
          webview.getSettings().setBuiltInZoomControls(true);
          webview.setVerticalScrollBarEnabled(false);
          webview.setHorizontalScrollBarEnabled(false);
          webview.getSettings().setAllowFileAccess(true);

          url  "http://www.klasrover.com/him&!485767890/1/1.mp4";

          if (url.endsWith(".mp4")) {     
              Intent intent  new Intent(Intent.ACTION_VIEW); 
              intent.setDataAndType(Uri.parse(url), "video/*");
              webview.getContext().startActivity(intent);   
          } else {
                webview.loadUrl(url);
          }
       }
}

Here is the layout page of webview:

<?xml version"1.0" encoding"UTF-8"?>
<RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:tools"http://schemas.android.com/tools"
    android:layout_width"match_parent"
    android:layout_height"match_parent"
    tools:context".WebviewActivity" >

    <WebView
        android:id"@+id/webview"
        android:layout_width"fill_parent"
        android:layout_height"fill_parent" />

</RelativeLayout>

Here, mp4 video is opened:

          if (url.endsWith(".mp4")) {
              Intent intent  new Intent(Intent.ACTION_VIEW); 
              intent.setDataAndType(Uri.parse(url), "video/*");
              webview.getContext().startActivity(intent);   
          } else {

But, when user wants to exit from the video and clicks to back button of the phone, there is a blank page. And if user clicks to back button again there is the video list again. However, what I want to do is, when user clicks to back button in order to exit from the video, there should be the video list again, not the blank page. What am I doing wrong? Is there any other way to open a mp4 video in webView? Thank you for your help.

Solution

Try this, work to mp3 audio and video mp4, in your case just ignore mp3 or delete:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp3")) {
            Intent intent  new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "audio/*");
            view.getContext().startActivity(intent);   
            return true;
        } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
                Intent intent  new Intent(Intent.ACTION_VIEW); 
                intent.setDataAndType(Uri.parse(url), "video/*");
                view.getContext().startActivity(intent);   
                return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
     }

Answered By – josedlujan

Leave a Comment