java WebView 不打开 android 默认视频播放器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6247146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:00:48  来源:igfitidea点击:

WebView NOT opening android default video player?

javaandroidwebview

提问by Kris

when I view this pageon my android device' default web browser and click the first video, it triggers the default video player of my device. It loads and play.

当我在我的 android 设备的默认网络浏览器上查看此页面并单击第一个视频时,它会触发我设备的默认视频播放器。它加载和播放。

However when I view the same link in my app, using a WebView, it does not open the default video player. What could be the problem?

但是,当我使用 WebView 在我的应用程序中查看相同的链接时,它不会打开默认的视频播放器。可能是什么问题呢?

I'm using the webview code in this link.

我正在使用此链接中的 webview 代码。

I also made the webview in full screen mode like what was said in the docs, is used this code to go fullscreen:

我还像文档中所说的那样以全屏模式制作了 webview ,使用此代码全屏显示:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

EDIT: I'm now using the following code, but still not work, any ideas?

编辑:我现在正在使用以下代码,但仍然无法正常工作,有什么想法吗?

package com.example.Playmp4OnWebView;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class PlayMp4OnWebView extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

          WebView webview = new WebView(this);
          setContentView(webview);

          WebSettings webSettings = webview.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setSupportZoom(false);
          webSettings.setPluginsEnabled(true);
          webSettings.setAllowFileAccess(true);

          webview.setWebViewClient(new WebViewClient(){

              public boolean shouldOverrideUrlLoading(WebView view, String url){
                   if(url.endsWith(".mp4")){
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i); //warning no error handling will cause force close if no media player on phone.
                        return true;
                   }
                   else return false; 
              }});

       //This will load the webpage that we want to see
        webview.loadUrl("http://www.broken-links.com/2010/07/30/encoding-video-for-android/");

     }
}

采纳答案by Nathan Schwermann

You have to attach your own WebViewClientand override shouldOverrideUrlLoading()if you detect a URL with a supported video mimetype return true and then launch the default activity with the URL. Here is an untested sample.

如果您检测到具有受支持视频 mimetype 的 URL 返回 true,然后使用该 URL 启动默认活动,则必须附加您自己的WebViewClient并覆盖shouldOverrideUrlLoading()。这是一个未经测试的样本。

mWebView.setWebViewClient(new WebViewClient(){

public boolean shouldOverrideUrlLoading(Webview view, String url){
     if(url.endsWith(".mp4") || url.endsWith("some other supported type")){
          Intent i = new Intent(Intent.ACTION_VIEW);
          i.setData(Uri.parse(url));
          startActivity(i); //warning no error handling will cause force close if no media player on phone.
          return true;
     }
     else return false; 
}});

hope this helps.

希望这可以帮助。

回答by Big-M

If you are getting a html5video tag in the WebViewthen the above code (using WebViewClient) will not be called, instead you will have to handle it with WebChromeClientas shown in the bellow link

如果您在 中获得html5视频标签,WebView则不会调用上面的代码(使用 WebViewClient),而是必须WebChromeClient按照下面的链接所示进行处理

How to play a video in a webview with android?

如何使用android在webview中播放视频?

I have tried it out and it worked well :)

我试过了,效果很好:)

the above code (using WebViewClient) is useful only when we have to handle a redirect request to another page via user click on anchor tag or through any hyperlink in the WebView

上述代码(使用 WebViewClient)仅在我们必须通过用户单击锚标记或通过任何超链接处理重定向请求到另一个页面时才有用 WebView