简体   繁体   English

如何在Android webview中打开mp4视频?

[英]How to open a mp4 video in Android webview?

In my application, there is a list of some videos. 在我的应用程序中,有一些视频列表。 I used listView for that. 我使用listView。 When a user clicks to a list element, webViewActivity is starting and I just want to open a "mp4" file in this webView. 当用户点击列表元素时,webViewActivity正在启动,我只想在此webView中打开"mp4"文件。 I came up with this solution: 我想出了这个解决方案:

WebViewActivity Class: WebViewActivity类:

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: 这是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: 在这里,mp4视频打开:

          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? 有没有其他方法在webView中打开mp4视频? Thank you for your help. 谢谢您的帮助。

Try this, work to mp3 audio and video mp4, in your case just ignore mp3 or delete: 试试这个,工作到mp3音频和视频mp4,在你的情况下只是忽略mp3或删除:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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