我试图在Android WebView中使用youtube iframe播放器,但在5.x上它在5.x上失败,一切都很好。
当我在4.x中尝试时,播放器会加载视频,当我开始播放时,我只看到视频声音。
logcat反复填充铬错误消息:
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的方法:
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);
另外,我在清单中启用硬件加速:
<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中? 感谢。
答案 0 :(得分:2)
我遇到了同样的问题,但经过这么长时间的战斗,我没有找到解决方案。我使用webview测试了所有配置,并进行了许多不同的html更改以放置iframe。
在我研究之后,我只能添加:
在Android 4版本中,无法自动播放。用户必须按播放按钮进入iframe才能开始播放视频。特别:
从Android 5和+一切正常。
祝你好运,我加上投票这个问题,知道是否有人可以解释这个问题:)