Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WebView未打开android默认视频播放器?_Java_Android_Webview - Fatal编程技术网

Java WebView未打开android默认视频播放器?

Java WebView未打开android默认视频播放器?,java,android,webview,Java,Android,Webview,当我在android设备的默认web浏览器上查看并单击第一个视频时,它会触发设备的默认视频播放器。它装载和播放 但是,当我在应用程序中使用WebView查看同一链接时,它不会打开默认的视频播放器。有什么问题吗 我正在中使用webview代码 我还将webview设置为全屏模式,如中所述,使用以下代码全屏显示: requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams

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

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

我正在中使用webview代码

我还将webview设置为全屏模式,如中所述,使用以下代码全屏显示:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
编辑:我现在正在使用以下代码,但仍然不工作,有什么想法吗

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/");

     }
}

如果检测到带有受支持的视频mimetype返回true的URL,然后使用该URL启动默认活动,则必须附加自己的URL并覆盖
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; 
}});

希望这有帮助。

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

我已经试过了,效果很好:)


以上代码(使用WebViewClient)仅在我们必须通过用户单击锚定标记或通过
WebView

Hi@schwiz中的任何超链接处理重定向请求到另一个页面时有用,非常感谢您的回答,我用更新的代码和您的建议代码更新了问题。但它仍然不会触发我设备的默认媒体播放器。有什么想法吗?@Kris如果它在同一个网络视图中加载,而不是启动活动或强制关闭,那么URL不能以mp4结尾,我会在shouldOverrideUrlLoading中设置一个断点,看看那里发生了什么。当我点击视频时,没有发生任何事情,它不会加载或启动任何活动或强制关闭。实际上,第一次单击vid im并没有使用href,而是使用html5标记。所以它认为它不会检测到您的条件url.endswith(“.mp4”)。关于如何检测点击标签以启动默认视频播放器,有什么想法吗?不管怎样,我现在使用android默认视频视图来播放SD卡上的视频。。。谢谢:)@Kris我能给你的最好建议是在日志中添加一个断点或打印URL,看看你在点击什么,然后想办法测试一下。