【发布时间】:2021-04-10 20:22:49
【问题描述】:
我正在开发一个带有 QR 码阅读器的 Cordova 应用程序。它通过使用 WebRTC API 工作。我试图让它自动播放而不需要用户与页面交互。但它只在某些时候有效,大多数时候它不会自动播放,并且会显示带有播放按钮的难看的灰色方块。
这是到目前为止所做的。
视频元素静音,设置为自动播放和内联播放:
const videoElement = document.createElement('video');
videoElement.style.width = `${width}px`;
videoElement.muted = true;
videoElement.playsInline = true;
videoElement.autoplay = true;
return videoElement;
视频开始播放后,play() 方法会在 1 秒后执行:
let Element = $('video')[0];
Element.autoplay = true;
setTimeout(() => { Element.play() }, 1000);
需要用户交互的 Cordova WebView 设置设置为 false。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
WebView webView = (WebView) appView.getEngine().getView();
WebSettings webSettings = webView.getSettings();
webSettings.setMediaPlaybackRequiresUserGesture(false); // <-- This is the important part
}
尽管如此,相机流在创建后仍然不会自动播放。
【问题讨论】:
-
@Eric 这并非一直有效。我仍然面临我所描述的问题。
标签: android cordova html5-video