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

标签 android video android-webview mp4

在我的应用程序中,有一些视频的列表。我为此使用了 listView。当用户单击列表元素时,webViewActivity 正在启动,我只想在此 webView 中打开一个 "mp4" 文件。我想出了这个解决方案:

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

这里是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>

这里打开了mp4视频:

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

但是,当用户想要退出视频并点击手机的后退按钮时,会出现一个空白页面。如果用户再次点击后退按钮,则会再次出现视频列表。但是,我想要做的是,当用户单击后退按钮以退出视频时,应该再次出现视频列表,而不是空白页面。我究竟做错了什么?有没有其他方法可以在 webView 中打开 mp4 视频?感谢您的帮助。

最佳答案

试试这个,工作到 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);
        }
     }

关于android - 如何在 Android webview 中打开 mp4 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24534632/

相关文章:

android - Javascript 接口(interface)类中的 SetText

Android: toast 消息消失后​​如何关闭应用程序?

android - Flutter是否有可用的Play核心库?

Android WebView在某些手机上无法显示html表格的右边框

audio - 将音频kb/s不一致的TS文件合并为一个音频一致的文件

hadoop - 使用 hadoop 快速编码我的视频

android - 如何在 webview android 6 中打开 pdf

java - 为什么在根据 stackoverflow 帖子(嵌入式)使用修复后,我现在收到无法解析方法长度错误?

android - 无法更改组所有者(组所有者 Intent )

video - ffmpeg从指定时间段缓慢转换视频