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

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

问题描述

当我查看此页面在我的Andr​​oid设备默认的Web浏览器,单击第一个视频,它触发我的设备的默认视频播放器。它加载和播放。

然而,当我看到相同的链接在我的应用程序,使用web视图,它不会打开默认的视频播放器。可能是什么问题?

我使用的WebView code中的这个链接

我也发了的WebView在全屏模式下喜欢什么在文档有人说,使用该code去全屏:

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

编辑:我现在用下面的code,但仍然无法正常工作,任何想法

 包com.example.Playmp4OnWebView;

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

公共类PlayMp4OnWebView延伸活动{
     / **第一次创建活动时调用。 * /
     @覆盖
     公共无效的onCreate(包savedInstanceState){
          super.onCreate(savedInstanceState);

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

          的WebView的WebView =新的WebView(本);
          的setContentView(web视图);

          WebSettings webSettings = webview.getSettings();
          webSettings.setJavaScriptEnabled(真正的);
          webSettings.setSupportZoom(假);
          webSettings.setPluginsEnabled(真正的);
          webSettings.setAllowFileAccess(真正的);

          webview.setWebViewClient(新WebViewClient(){

              公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
                   如果(url.endsWith(。MP4)){
                        意图I =新的意图(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(URL));
                        startActivity(ⅰ); //警告没有错误处理将导致强制关闭,如果在电话中没有任何的媒体播放器。
                        返回true;
                   }
                   否则返回false;
              }});

       //这将加载,我们希望看到的网页
        webview.loadUrl(http://www.broken-links.com/2010/07/30/encoding-video-for-android/);

     }
}
 

解决方案

您必须附上自己的 WebViewClient 并覆盖 shouldOverrideUrlLoading()如果发现网址有支持的视频MIMETYPE返回true,然后启动默认的活动的URL。下面是一个未经测试的样本。

  mWebView.setWebViewClient(新WebViewClient(){

公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
     如果(url.endsWith(MP4)|| url.endsWith(其他支持的类型)){
          意图I =新的意图(Intent.ACTION_VIEW);
          i.setData(Uri.parse(URL));
          startActivity(ⅰ); //警告没有错误处理将导致强制关闭,如果在电话中没有任何的媒体播放器。
          返回true;
     }
     否则返回false;
}});
 

希望这会有所帮助。

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.

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'm using the webview code in this link.

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

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

     }
}

解决方案

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

hope this helps.

这篇关于的WebView不开放的Andr​​oid默认视频播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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