如何在QtWebEngine(使用QML插件时)使用WebRTC网络摄像头请求?
webengine.qml
import QtQuick 2.1
import QtQuick.Controls 1.1
import QtWebEngine 1.0
ApplicationWindow {
width: 800
height: 600
color: "lightgray"
visible: true
WebEngineView {
id: webview
url: "https://opentokrtc.com/test"
anchors.fill: parent
}
}
在我的Mac Yosemite上,运行命令:
/usr/local/Cellar/qt5/5.4.0/bin/qmlscene webengine.qml
但视频无法启动,因为它正在等待“允许”相机
在浏览器上你有这个
有没有办法以编程方式设置Chromium Web Engine政策,例如VideoCaptureAllowed
答案 0 :(得分:3)
您需要使用 QtWebEngine.experimental
请尝试此操作。
import QtQuick 2.4
import QtQuick.Window 2.2
import QtWebEngine 1.0
import QtWebEngine.experimental 1.0
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Private 1.0
Window {
visible: true
WebEngineView {
id: webEngineView
url: "https://test.webrtc.org/"
anchors.fill: parent
anchors.margins: 10
experimental.onFeaturePermissionRequested: {
console.log("request")
experimental.grantFeaturePermission(securityOrigin, feature, true);
}
readonly property string hideElementsJS: "
function hideElement(id) {
const el = document.getElementById(id);
if (el) {
el.style.display = 'none';
}
}
function hideElementsByClass(className) {
const elList = document.getElementsByClassName(className);
for (var i = 0, n = elList.length; i < n; ++i) {
elList[i].style.display = 'none';
}
}
hideElement('hnArea');
hideElement('lxSocialBarWrapper');
hideElement('footerContent');
hideElement('ftDisclaimers');
hideElement('bottomNav');
hideElement('topLinks');
hideElement('rightMenuButtons');
hideElementsByClass('footerText');
hideElementsByClass('disclaimers');
"
onLoadingChanged: {
if(loadRequest.status === WebEngineView.LoadSucceededStatus) {
console.log("start")
runJavaScript(hideElementsJS);
console.log("stop")
}
}
}
}
答案 1 :(得分:2)
将此添加到您的WebEngineView项目,以从所有来源授予任何请求的功能,或者可选地将其限制为特定的来源和特定功能:
onFeaturePermissionRequested: {
grantFeaturePermission(securityOrigin, feature, true);
}
答案 2 :(得分:1)
打开 fancybrowser 项目 在函数MainWindow :: MainWindow(const QUrl&amp; url)中添加mainwindow.cpp
connect(view->page(), SIGNAL(featurePermissionRequested(QUrl,QWebEnginePage::Feature)),SLOT(test(QUrl,QWebEnginePage::Feature)));
还要添加
void MainWindow::test(QUrl q, QWebEnginePage::Feature f) { view->page()->setFeaturePermission(q, f, QWebEnginePage::PermissionGrantedByUser); }
在mainwindow.cpp和mainwindow.h下面
受保护的插槽:
void test(QUrl q, QWebEnginePage::Feature f);
然后一切正常!!