【问题标题】:Android webview black screen while playingAndroid webview播放时黑屏
【发布时间】:2018-05-02 04:26:26
【问题描述】:

我正在开发一个使用 WebView 加载外部视频 (.mp4) 的应用,但页面只播放音频,并且视频只是“黑色”。我已经搜索了很多,并做了所有可能的事情来尝试解决这个问题,但我失败了。你能帮帮我吗?

MainActivity.java

-- REMOVED --

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="topflix.topflix">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/rounded"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:hardwareAccelerated="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>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="topflix.topflix.MainActivity">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/wv"
    />
</android.support.constraint.ConstraintLayout>

activity_main.xml的打印>点击here


视频网址:http://media-br-am.crackle.com/1/3/v6/11zlf_480p.mp4 测试网站:ntcdn.stream/prop/httpdelivery/modal
我已经做了什么:

  • 设置 hardwareAccelerated=true
  • 设置 wv.setWebChromeClient(new WebChromeClient());
    并且视频屏幕持续为黑色,仅运行音频。我该怎么办??

【问题讨论】:

  • 发布您尝试在 webview 中加载的视频网址
  • 线程更新了!谢谢
  • 在下面查看我的答案
  • 并非所有视频格式都受支持。可能您的视频格式不受支持。(至少在您的设备上)
  • 不敢相信我的 J5 无法加载 .mp4 视频:\

标签: android android-studio html5-video html5-audio


【解决方案1】:

下面的代码可以让我在 webview 中加载视频:

webView = findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

        if (Build.VERSION.SDK_INT >= 21) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }

        if (android.os.Build.VERSION.SDK_INT < 16) {
            webView.setBackgroundColor(0x00000000);
        } else {
            webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
        }

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(request.getUrl().toString());
                }
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageStarted(WebView webview, String url, Bitmap favicon) {
                super.onPageStarted(webview, url, favicon);
                webview.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onPageFinished(WebView webview, String url) {

                webview.setVisibility(View.VISIBLE);
                super.onPageFinished(webview, url);

            }
        });
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

        webView.loadUrl("http://media-br-am.crackle.com/1/3/v6/11zlf_480p.mp4");

编辑:如果您的视频在 iframe 中 -->

webView.loadDataWithBaseURL(null, iframe, "text/html; charset=utf-8", "utf-8", null);

请看我附上的截图:

【讨论】:

  • 我在播放包含视频的HTML 页面时遇到类似问题,只播放音频但不播放视频。有关我的案例的更多详细信息,您可以通过此链接查看我的问题stackoverflow question
  • @Ebraheem 你检查过我用 iframe 编辑的答案吗?
  • 感谢您的回复,但在我的情况下,HTML 页面中没有 iframe
  • @Ebraheem 我的意思是,如果有任何 HTML 内容,你可以使用 iframe 方法。
  • 感谢您的宝贵时间和帮助。我终于弄明白了,这是一个安卓模拟器相关的问题,当我收到我的安卓电视盒并在上面测试应用程序时,一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多