播放时Android webview黑屏 [英] Android webview black screen while playing

查看:118
本文介绍了播放时Android webview黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

MainActivity.java

-- 删除--

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><申请机器人:allowBackup =真"android:icon="@drawable/rounded"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"安卓:硬件加速=真"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><意图过滤器><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动></应用程序></清单>

activity_main.xml

<网页视图android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/wv"/></android.support.constraint.ConstraintLayout>

activity_main.xml 的打印> 点击

I'm developing an app that uses WebView to load external video (.mp4), but the page plays only audio, and the video is only "black". I've searched so much and did all possible things to try solve this problem but I failt. Can you help me?

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>

Print of activity_main.xml > click here


Video URL: http://media-br-am.crackle.com/1/3/v6/11zlf_480p.mp4 Website for tests: ntcdn.stream/prop/httpdelivery/modal
What I already did:

  • Set hardwareAccelerated=true
  • Set wv.setWebChromeClient(new WebChromeClient());
    and the video screen persists in black, running only audio. What should I do??

解决方案

The below code is working for me to load video in 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");

EDIT : if your video is in iframe -->

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

see the screenshot i attached :

这篇关于播放时Android webview黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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