【问题标题】:Android webview cannot play video file errorAndroid webview 无法播放视频文件错误
【发布时间】:2020-05-06 16:08:49
【问题描述】:

在我的应用程序的一个屏幕中,我将 HTML 字符串加载到 WebView 中,但由于某种原因,WebView 无法在 div 中加载视频,视频容器显示错误: 无法播放此视频文件:错误代码 102003 这是正在加载的 HTML 示例,其中的视频无法加载:

 <html dir="rtl" lang=""><body><meta itemprop="thumbnailUrl" content="https://cdn.jwplayer.com/v2/media/8K9oJcsX/poster.jpg?width=720"/><meta itemprop="contentUrl" content="https://cdn.jwplayer.com/videos/8K9oJcsX-khorc1ya.mp4"/><div style="position:relative; overflow:hidden;"><script src="https://cdn.jwplayer.com/players/8K9oJcsX-4mQgHT7J.js"></script></div></div> <p>تابعي نصائح وأفكار دليل مطبخ سيدتي التي ستساعدك في الحصول على مائدة رمضانية فاخرة ومميزة حتى في أيام الحجر المنزلي.</p></body></html>

我启用了 javascript,添加了 hardwareAcclerated=true 添加了 chrome 客户端以及其他问题中建议的所有其他内容。

val settings = webview.settings
            settings.domStorageEnabled = true
            settings.javaScriptEnabled = true
            settings.pluginState = WebSettings.PluginState.ON
            webview.webChromeClient = WebChromeClient()
            webview.webViewClient = WebViewClient()

            settings.setDomStorageEnabled(true);
            settings.setAppCacheEnabled(true);
            settings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
            settings.setDatabaseEnabled(true);
            settings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");

            settings.allowFileAccess = true
            val formattedHtml = "<html dir=\"rtl\" lang=\"\"><body>" + tip.description + "</body></html>"
            webview.loadDataWithBaseURL("", formattedHtml, "text/html", "UTF-8", "")

【问题讨论】:

    标签: android html video webview


    【解决方案1】:

    刚刚在我的 webview 中尝试了您的视频网址,它有效。这是我的设置:

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
    

    所以我猜你必须将 setMediaPlaybackRequiresUserGesture 设置为 false。

    【讨论】:

    • 我使用 webView.loadUrl 尝试了你的 url,所以我认为 loadDataWithBaseURL 是导致问题的原因。
    【解决方案2】:

    问题是缺少基本 URL,这导致 JWPlayer(视频主机)抛出错误。

    正确的做法是添加本地android_asset base URL

    tipInformation.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "UTF-8", null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 2012-12-14
      • 2013-10-02
      相关资源
      最近更新 更多