簡體   English   中英

如何在Android webview中打開mp4視頻?

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

在我的應用程序中,有一些視頻列表。 我使用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);
        }
     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM