Android WebView视频播放,音频正常,但未显示图像-在浏览器中可以 [英] Android WebView video plays, audio fine, but shows no image - ok in browser

查看:1334
本文介绍了Android WebView视频播放,音频正常,但未显示图像-在浏览器中可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Android WebView中播放视频时,我有一个奇怪的行为. 视频确实可以播放,并且声音可以听见,但是视频应该只有白/黑屏. 使用本机浏览器,它可以很好地工作.

I have a strange behaviour when trying to play back a video in an Android WebView. Whilst the video actually does play, and the sound is audible, there's only a white/ blank screen where the video should be. It works perfectly fine using the native browser.

即使在非常基本的新Android项目(使用新的空模拟器)中也不会显示视频.还尝试使用3gp格式并使用一些最新的SDK(API 27). 在仿真器(各种设备)和物理设备之间也没有区别.打开包含视频的其他网站也没有成功(显示网站以及视频-可以播放声音,但没有图像).

Not even in a new very basic, new Android project (using a new, empty emulator), a video is displayed. Also tried with the 3gp format and using some recent SDK (API 27). Also there is no difference among emulator (various devices) and the physical device. I also had no success opening different websites containing videos (websites are displayed, together with the video - sound available, but no image).

视频控件可见并且可以单击(除非设置为自动播放,否则将立即播放).

The video control is visible and can be clicked (unless set to autoplay, where it is immediately played back).

日志中的错误消息:

E/chromium: [ERROR:gl_surface_egl.cc(264)] eglChooseConfig failed with error EGL_SUCCESS
E/EGL_emulation: eglQueryContext 32c0  EGL_BAD_ATTRIBUTE
E/EGL_emulation: tid 23192: eglQueryContext(1772): error 0x3004 (EGL_BAD_ATTRIBUTE)
E/ACodec: [OMX.google.h264.decoder] setPortMode on output to DynamicANWBuffer failed w/ err -1010

WebView中显示的HTML页面:

HTML page displayed in the WebView:

<html>
<head>
</head>
<body>
<video controls autoplay>
    <source src="https://www...*.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>
</body>
</html>

基本应用内容:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = findViewById(R.id.webView);
        webView.loadUrl("https://www...");
    }
}

我的尝试包括设置自定义Webview,Webchrome客户端以及启用/禁用自定义设置.

My attempts include setting custom webview, webchrome client as well as enabling/ disabling custom settings.

webView.setWebChromeClient(new MyWebChromeClient());
webView.setWebViewClient(new MyWebViewClient(activity));

webView.clearCache(true);
webView.clearHistory();
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true); // Seems to be required to support HTTPS
webSettings.setAllowFileAccess(true);
//Enabling zoom-in controls
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);

webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

在清单中:

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

尝试

android:hardwareAccelerated="true"/ "false"

我们将不胜感激.

推荐答案

显然,网络引擎无法再计算视频大小.然后将其宽度和高度设置为零.

Apparently, the video size could not be calculated by the web engine anymore. It was then rendered with width and height of zero.

这可以通过为<video>标签分配宽度来解决:

This can be fixed by assigning a width to the <video> tag:

<video controls autoplay style="width:100px;">
    <source src="https://www...*.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

<div style="width:100%; height:100%;">
  <video controls autoplay>
    <source src="https://www...*.mp4" type="video/mp4">
      Your browser does not support the video tag.
  </video>
</div>

这篇关于Android WebView视频播放,音频正常,但未显示图像-在浏览器中可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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