gpt4 book ai didi

javascript - HTML5 视频不会在 Amazon WebView 中自动启动

转载 作者:行者123 更新时间:2023-11-29 20:59:43 24 4
gpt4 key购买 nike

我正在尝试使用 Amazon WebView 组件为 Amazon FireTV 开发视频应用程序。我无法让视频自动播放,我已经尝试使用 HTML5 视频 DOM 属性和事件,还设置了视频标签属性 autoplay="autoplay"。这是 HTML 代码:

<!DOCTYPE html>
<html>
<head>
<title>Test player</title>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function () {
console.log('DOM Loaded!');
var player = document.getElementById('video-player');
player.autostart = true;
player.src = "http://www.w3schools.com/html/mov_bbb.mp4";
player.load();
player.addEventListener("canplay", function () { console.log('play'); document.getElementById('video-player').play(); }, false);
}, false);
</script>
</head>
<body>
<video style="background: #e0e0e0; width: 400px; height: 320px;" id="video-player" controls="controls" src="" autoplay="autoplay" >Your browser does not support Video rendering</video>
<br />
</body>
</html>

初始化WebView的Java代码如下(在onCreate方法中):

this.wrapper = (AmazonWebView) findViewById(R.id.wrapper);
factory.initializeWebView(this.wrapper, 0xFFFFFF, false, null);

this.wrapper.setFocusable(false); // Never allow focus on the WebView because it blocks the Play/Pause, Fast Forward and Rewind buttons

AmazonWebChromeClient webClient = new AmazonWebChromeClient();

this.wrapper.setWebChromeClient(webClient); // Needed in order to use the HTML5 <video> element
AmazonWebSettings browserSettings = this.wrapper.getSettings();
browserSettings.setPluginState(AmazonWebSettings.PluginState.ON);
browserSettings.setJavaScriptEnabled(true);
this.wrapper.addJavascriptInterface(new JSChannel(this.wrapper), "appChannel");
browserSettings.setUseWideViewPort(true);

我还在 list 中设置了互联网权限和硬件加速。我是否需要向 Java 代码添加一些内容才能自动播放视频?

最佳答案

FireTV 与一般的 Android 一样,不支持自动播放 HTML5 中的视频(是的,我知道这是一种痛苦)

要解决这个问题,您需要通过 Javascript 桥从容器 apk 触发 video.play() 消息(我这样做是为了响应 HTML 中的 loadedmetadata 事件,不过您可以,例如,使用 canPlayThrough或其他触发它的事件)

在 Java 中:

@JavascriptInterface
public void vidLoaded() {
WebViewActivity.this.runOnUiThread(new Runnable() {
public void run() {
webView.loadUrl("javascript: document.getElementById(\"video-player\").play();");
}
});
Log.v(TAG, "Start playing video via Java Bridge");
}

在您的 HTML 中:

document.getElementById("video-player").addEventListener('loadedmetadata', vidLoad, false);
....
function vidLoad() {
document.getElementById("video-player").removeEventListener('loadedmetadata', vidLoad, false)
// use a Java method to trigger "play" on the video via JavaScriptInterface
appChannel.vidLoaded();
}

关于javascript - HTML5 视频不会在 Amazon WebView 中自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com