WebView 不打开 android 默认视频播放器? [英] WebView NOT opening android default video player?

查看:31
本文介绍了WebView 不打开 android 默认视频播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

我正在使用 此链接.

我也像文档中所说的那样以全屏模式制作了网页视图,是用这个代码去全屏的:

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

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

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

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

     }
}

推荐答案

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

You have to attach your own WebViewClient and 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.

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 不打开 android 默认视频播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆