如何在 WebView 中打开视频播放器? [英] How to open video player in WebView?

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

问题描述

我有一个带有 Webview 的 WebApp.在这些页面中,我有一些视频链接(MP4、3GP...).

I've a WebApp showed with Webview. In those pages I've some links to videos (MP4, 3GP...).

当我点击链接时,什么也没有发生.?

When I click on the link, nothing happens.?

public class luxgateway extends Activity {

WebView myWebView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);     

    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setAllowFileAccess(true); 

    WebViewClient webViewClient = new MyWebViewClient();
    myWebView.setWebViewClient(webViewClient);

    myWebView.loadUrl("http://www.example.com/demo.html");
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

private class MyWebViewClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".3gp")) {
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
            view.getContext().startActivity(intent);    
            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
}

我在这台服务器上找到了有关此代码的一些部分,但对我没有帮助"

I've found some parts on this code on this server, but it doesn't help me"

        if (url.endsWith(".3gp")) {
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
            view.getContext().startActivity(intent);    
            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }

在这种情况下,我仅限于 3GP 文件,但我不知道网站上将放置哪个扩展名.

In this case I'm limited to 3GP files, but I'don't know which extension will be place on the WEBsite.

我已经在测试中尝试了此代码,只是为了查看视频是否可以播放(或视频播放器仅以此代码开头

I've tried this code thisout the test, only to see if the video could be played (or videoplayer start with only this code

                Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
            view.getContext().startActivity(intent);    
            return true;

在这种情况下,视频仅被下载.我希望将示例交互作为 webrowser.当我点击视频链接时,浏览器会询问我想使用哪个播放器播放此视频.

In this case, the video is only dowloaded. I want to have the sample interaction as the webrowser. When I click on the video link, the browser ask which player I want to user for this video.

推荐答案

如果您指定 uri 包含视频,您可能会有更好的运气:

You may have better luck if you specify that the uri contains video:

Intent intent = new Intent(Intent.ACTION_VIEW) //I encourage using this instead of specifying the string "android.intent.action.VIEW"
intent.setDataAndType(Uri.parse(url), "video/3gpp");
view.getContext().startActivity(intent);  

因为我们没有指定具体的包来处理上述意图,所以称为隐式意图.对于隐式意图,如果有多个包处理您指定的内容类型,您将被要求在它们之间进行选择.这应该会给你你想要的行为.

Because we haven't specified a specific package to handle the above intent, it is called an implicit intent. For implicit intents, if there are multiple packages that handle the content type that you specify, you will be asked to choose between them. This should give you your desired behavior.

这篇关于如何在 WebView 中打开视频播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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