【问题标题】:How to Allow Webview Accessing Online Video Using Android Video Player Application?如何允许 Webview 使用 Android 视频播放器应用程序访问在线视频?
【发布时间】:2020-02-23 11:38:24
【问题描述】:

所以,我有一个带有链接视频的网络服务器,我可以访问我的网络视图,我可以使用 HTML5 播放器播放视频,但问题是,视频必须由视频播放安装在我手机上的播放器应用程序... 我如何做到这一点?

我已经尝试过搜索,人们似乎喜欢使用 Youtube 视频播放器或 HTML5 播放器,而不是使用他们的视频播放器应用程序,所以我什么都没有......

更新

所以,在我尝试实现@EricHo建议的方法之后

webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("mp4")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse(url), "video/mp4");
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });

我得到的是,播放视频的播放器是Chrome内置的,而不是Android视频播放器本身Photo 1,除了我期望的是The video will be played by gallery instead of Chrome player

如果需要,这里是我的 index.html 源代码

<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
    <title>video</title>
    <script type="text/javascript" src="src.js"></script>
    <link rel="stylesheet" type="text/css" href="src.css"> 
</head>
<body bgcolor="#333">
<center>
<video controls preload="metadata" style=" width:px;  height:px;">
    <source src="video-url" type="video/mp4">
</video><br />

</body>
</html>

【问题讨论】:

  • 从您的网站解析视频 URL,并以 URL 作为数据开始操作意图 Intent.ACTION_VIEW

标签: java android video android-webview android-videoview


【解决方案1】:

一种方法是拦截来自 Webview 的 HTTP 请求。 refer to this

然后检查请求 URL 是否为视频 url。如果是,请使用 Intent 打开网址

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(videoPath), "video/mp4");
startActivity(intent);

编辑: 下面是我的工作示例,当您单击以“mp4”结尾的链接时,android 将使用默认媒体播放器打开该 url。

    webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            println("shouldOverrideUrlLoading")
            println(request?.url.toString())
            val url :String = request?.url.toString()
            if (url.endsWith("mp4")) {
                val intent = Intent(Intent.ACTION_VIEW)
                intent.setDataAndType(Uri.parse(url), "video/mp4")
                startActivity(intent)
                return true
            }
            return false
        }
    }
    val videoPath = "http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5"
    webView.loadUrl(videoPath)

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ecl.webtest">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

【讨论】:

  • 那么你的意思是我需要列出 youtube 链接、vimeo 链接等链接来进行比较?
  • 也许你检查链接是否以“.mp4”结尾,然后将链接传递给Intent(Intent.ACTION_VIEW)以在android中使用视频播放器打开
  • 好吧,我先试试这个,好不好我会更新
  • 好吧,我卡在这里了,我声明 WebViewClient.shouldInterceptRequest(webview,url) 后如何显示 HTTP 请求?
  • stackoverflow.com/a/4805033/6854435 建议你试试这段代码,url可以通过覆盖#shouldOverrideUrlLoading()方法访问
【解决方案2】:

我可能是错的,但如果视频下载到手机上的 tmp 文件夹或其他文件夹中,则您的默认 android 播放器会播放视频。您似乎可以使用 html 视频控件在您的页面上播放链接的视频,但它不会与您的 android 播放器一起播放,因为它没有被下载并被识别为视频文件。您可以在页面上创建一个超链接,将视频链接作为 url,让我们看看默认播放器是否会选择它

【讨论】:

  • 好吧,问题是视频可以是任何网络视频,所以不能使用静态超链接
猜你喜欢
  • 1970-01-01
  • 2016-10-10
  • 2011-06-26
  • 2012-12-16
  • 2019-03-23
  • 2015-12-07
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多