html5视频不适用于android webview

时间:2019-03-15 02:36:52

标签: android html5 webview uiwebview html5-video

我已经在webview中运行android应用一年了。 但是有些客户说几天前html5视频没有在我的应用程序中播放。 我有五个要测试的手机,而且我所有的手机都没错。大多数客户使用该应用程序都没有任何问题。

我将preload属性值从auto更改为meta,因为我想由于网络负载而造成的麻烦。

下面的图片是客户的屏幕截图。 时间为27:10,没有用于加载的微调器。 (我认为加载已结束) 但是,客户无法按下播放按钮,并且当按下播放按钮时,视频元素周围会出现一条橙色线。

enter image description here

客户的手机是Galaxy Note 8和Galaxy J7,Android版本是8。但是在我的测试中没有问题。并且有使用该服务的Android 8客户没有任何问题。

我该如何解决?谢谢。

html

<video preload="meta" width="95%" autoplay="autoplay" controls="" playsinline="">               
        <source type="video/mp4" src="url.../file.mp4">
        HTML5 is not supported.
    </video>

android-MainActivity

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private BackPressCloseHandler backPressCloseHandler = new BackPressCloseHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);
        /* webView settings */
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebChromeClient(new FullscreenableChromeClient(MainActivity.this));
        webView.setWebViewClient(new WebViewClientClass());

        webView.loadUrl("MY URL IS HERE");
    } //onCreate

    private class WebViewClientClass extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

自定义类别以启用全屏视频

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.widget.FrameLayout;

public class FullscreenableChromeClient extends WebChromeClient {
    private Activity mActivity = null;

    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    private int mOriginalOrientation;
    private FrameLayout mFullscreenContainer;
    private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    public FullscreenableChromeClient(Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }

            mOriginalOrientation = mActivity.getRequestedOrientation();
            FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
            mFullscreenContainer = new FullscreenHolder(mActivity);
            mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
            decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
            mCustomView = view;
            setFullscreen(true);
            mCustomViewCallback = callback;
//          mActivity.setRequestedOrientation(requestedOrientation);

        }

        super.onShowCustomView(view, callback);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onShowCustomView(View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback) {
        this.onShowCustomView(view, callback);
    }

    @Override
    public void onHideCustomView() {
        if (mCustomView == null) {
            return;
        }

        setFullscreen(false);
        FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
        decor.removeView(mFullscreenContainer);
        mFullscreenContainer = null;
        mCustomView = null;
        mCustomViewCallback.onCustomViewHidden();
        mActivity.setRequestedOrientation(mOriginalOrientation);

    }

    private void setFullscreen(boolean enabled) {

        Window win = mActivity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        if (enabled) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
            if (mCustomView != null) {
                mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }
        win.setAttributes(winParams);
    }

    private static class FullscreenHolder extends FrameLayout {
        public FullscreenHolder(Context ctx) {
            super(ctx);
            setBackgroundColor(ContextCompat.getColor(ctx, android.R.color.black));
        }
        @Override
        public boolean onTouchEvent(MotionEvent evt) {
            return true;
        }
    }
}

manifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:hardwareAccelerated="true"
        android:theme="@style/AppTheme">
        <activity android:name="....webview.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize">

        </activity>
        <activity android:name="....webview.IntroActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:1)

我在视频应用中遇到了与Android System Webview 73相同的问题。默认的html5视频播放器似乎存在一些样式问题。我可以执行自动播放,也可以在javascript中调用play()函数。

我的解决方案是使用其他一些javascript视频播放器。我使用videoJS替换了视频播放器界面,并且现在可以使用。

答案 1 :(得分:1)

以下代码对我来说很好用,请检查一次

将此行添加到清单文件中

extf = ['$RECYCLE.BIN','System Volume Information']

MainActivity

import os
i = 0
az = lambda: (chr(i)+":\\" for i in range(ord("D"), ord("Z") + 1))
for drv in az():
    for root, dirs, files in os.walk(drv):
        for filename in files:
            splitname = filename.split('.')
            if splitname[-1] !="vhdx":
                continue
            file_path = (os.path.join(root, filename))
            print file_path
            i += 1
    if i != 0:
        print ("total vhdx files:",i)

}

activity_main.xml

   <application android:hardwareAccelerated="true" ...>

public class MainActivity extends AppCompatActivity {

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = findViewById(R.id.webView);
    webview.setWebViewClient(new MyBrowser());
    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);
    webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
    webview.loadUrl("https://www.w3schools.com/html/html5_video.asp");
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

答案 2 :(得分:0)

由于使用javascript调用来播放/暂停视频作品,因此我在应用中修复了此问题,方法是在视频前面添加一个div并调整其大小以匹配Android放置在视频中心附近的播放按钮。这是React的一个片段:

const isAndroid = () => navigator.userAgent.indexOf('Android') >= 0;

let videoPlayer;

...

<div className="video-wrap">
  <video
    ref={node => videoPlayer = node}
    controls
    controlsList="nodownload"
    crossOrigin="anonymous"
    poster={episode.posterUrl}
  >
    <source src={episode.h264Url} type="video/mp4" />
    {episode.webmUrl ? (
      <source src={episode.webmUrl} type="video/webm" />
    ) : null}
    {episode.vttUrl ? (
      <track kind="captions" label="English" src={episode.vttUrl} srcLang="en" />
    ) : null}
  </video>

  { isAndroid() ? (
    <div
      className="android-playback-fix"
      onClick={e => {
        e.preventDefault();

        if (videoPlayer.paused) {
          videoPlayer.play();
        } else {
          videoPlayer.pause();
        }
      }}
    ></div>
  ) : null}
</div>

这是CSS:

.video-wrap {
  position: relative;
}

.android-playback-fix {
  border-radius: 100%;
  height: 75px;
  left: 50%;
  position: absolute;
  top: 45%;
  transform: translate(-50%, -50%);
  width: 75px;
}

答案 3 :(得分:0)

我遇到同样的问题。

要解决此问题。

在您的HTML视频标签上放置一个id,并在您的Java代码中调用该id

例如

<video id = "video" preload="meta" width="95%" autoplay="autoplay" controls="" playsinline=""><source type="video/mp4" src="url.../file.mp4"></video>

onPageFinished上-对WebViewClient的替代,输入以下代码

webView.loadUrl("javascript:(function() { document.getElementById('video').play(); })()");
webView.loadUrl("javascript:(function() { document.getElementById('video').pause(); })()");

这将注入javascript函数以允许自动播放视频