简体   繁体   中英

How to open a mp4 video in Android webview?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM