Youtube iframe 播放器不会在 android 4.x WebView 中显示视频 [英] Youtube iframe player won't show videos in android 4.x WebView
问题描述
我正在尝试在 android WebView 中使用 youtube iframe 播放器,但它在 android 4.x 上失败,而在 5.x 上一切正常.
当我在 4.x 中尝试时,播放器会加载视频,当我启动它时,除了听到视频声音之外什么也看不到.logcat 一遍又一遍地充满了 Chromium 错误消息:
I'm trying to use the youtube iframe player inside an android WebView but it fails on android 4.x while on 5.x everything works great.
When I try it in 4.x the player loads the video, when I start it I see nothing but hear the video sound.
The logcat gets filled with chromium error messages over and over again:
E/chromium(20362): [ERROR:gles2_cmd_decoder.cc(5942)] [.Compositor-Onscreen-0x5db83bb8]GL ERROR :GL_INVALID_OPERATION : glUseProgram: program not linked
E/chromium(20362): [ERROR:gles2_cmd_decoder.cc(5718)] [.Compositor-Onscreen-0x5db83bb8]GL ERROR :GL_INVALID_OPERATION : glUniformMatrix4fv: wrong uniform function for type
E/chromium(20362): [ERROR:gles2_cmd_decoder.cc(5718)] [.Compositor-Onscreen-0x5db83bb8]GL ERROR :GL_INVALID_OPERATION : glUniform1iv: wrong uniform function for type
以下是我设置 WebView 的方法:
Here's how I set the WebView:
this.webview = (WebView) this.findViewById(R.id.webview);
WebSettings settings = this.webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(false);
this.webview.setWebChromeClient(new WebChromeClient());
...
this.webview.loadUrl(ADDRESS_OF_PAGE);
此外,我在清单中启用了硬件加速:
Also, I enable hardware acceleration in the manifest:
<application
...
android:hardwareAccelerated="true">
这是js部分:
var player;
var VIDEO_ID = "EIsauUFIguE";
window.onYouTubeIframeAPIReady = function() {
console.log("youtube api ready, loading player");
player = new YT.Player("playerIframe", {
width: "100%",
height: "100%",
videoId: null,
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
onPlaybackQualityChange: onPlayerPlaybackQualityChange,
onError: onPlayerError
},
playerVars: {
modestbranding: 1,
playsinline: 1,
fs: 0,
showinfo: 0
}
});
}
function onPlayerReady(event) {
console.log("player is ready: ", event);
playVideo();
}
function onPlayerStateChange(event) {
console.log("player state change: ", event);
}
function onPlayerPlaybackQualityChange(event) {
console.log("player playback quality change: ", event);
}
function onPlayerError(event) {
console.log("player error: ", event);
}
function playVideo() {
console.log("playing video: " + VIDEO_ID);
player.loadVideoById(VIDEO_ID);
player.setPlaybackQuality("medium");
}
function loadPlayer() {
console.log("loading youtube api");
var tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
loadPlayer();
知道为什么它的行为方式如此且仅在 android 4.x 中?谢谢.
Any idea why it behaves this way and only in android 4.x? Thanks.
推荐答案
我遇到了同样的问题,但是经过这么长时间的斗争,我没有找到解决方案.我使用 webview 测试了所有配置,并做了许多不同的 html 更改以放置 iframe.
I had the same problem, but after fighting with this long time, i didn't find a solution. I tested all the configurations with webview and did many different html changes to put the iframe.
经过我的研究,我只能补充:
After my researching i only can add:
在 Android 4 版本中,不能自动播放.用户必须在 iframe 中按下播放按钮才能开始播放视频.特别是:
In Android 4 versions, can't autoplay. The user has to press play button into the iframe to start play the video. Particularly:
- 在 Android 4.1 中,它再现音频但不再现图像.
- 在 Android 4.3 中,我在可以再现音频但不能再现视频的设备和不能再现任何内容的设备中进行了测试.
- 在 Android 4.4 中不要复制任何内容
从 Android 5 和 + 开始,一切正常.
From Android 5 and + everything works fine.
祝你好运,我加上投票这个问题,想知道是否有人可以解决这个问题:)
Good luck and I plus vote this question to know if anyone can put light into this question :)
这篇关于Youtube iframe 播放器不会在 android 4.x WebView 中显示视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!