如何使用Qt WebEngineView授予从麦克风捕获音频功能的权限



我正在使用WebEngineView QML类型来呈现本地托管的网页,该网页使用Qt 5.15.1上的WebRTC HTML媒体音频捕获功能(该功能列在WebEngineView.可用的功能中

该网页使用nginx服务器在本地托管,当被其他浏览器访问时,它可以完美地工作。

使用WebEngineView访问麦克风时,麦克风没有捕获,很可能是因为没有授予这样做的权限。

使用WebEngineView打开网页时,控制台中不会出现任何类型的错误,只是不会捕获来自系统输入源的音频。

有人知道如何通过Qt WebEngineView或其他方式正确授予此特定功能权限吗?

以下是我的代码。

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtWebEngine 1.10
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
WebEngineView {
id: view
anchors.fill: parent
url: "http://127.0.0.1/audio.html"
Component.onCompleted: {

/* heres my attempt to grant permission 
to use the microphone, which did not 
seem to have any affect */
view.grantFeaturePermission("http://127.0.0.1",
MediaAudioCapture, true)                                        
}
}
}

有人知道如何通过Qt WebEngineView或其他方式正确授予此特定功能权限吗?

**编辑**

audio.html(相关代码(

<!DOCTYPE html>
<head></head><body><div id="results"></div><script>
window.rc = new webkitSpeechRecognition()
rc.continuous = true;

rc.onresult = function(result) {
document.querySelector("#results").innerHTML = result.results[0][0].transcript;
}
rc.start();


</script></body></html>

在qwebengineview中,每当请求访问相机/麦克风等功能时,都会发出以下信号。

featurePermissionRequested(url securityOrigin,Feature Feature(

因此,您可以在onFeaturePermissionRequested处理程序中为请求权限的功能授予权限。

例如,请尝试以下操作,了解如何自动授予功能权限。

WebEngineView {
id: webEngineView
anchors.fill: parent
url: "https://webcammictest.com/"
onFeaturePermissionRequested: {
webEngineView.grantFeaturePermission(securityOrigin, feature, true);
}
}

如果您想像在通常的web浏览器中那样添加功能权限授予弹出窗口,请尝试在代码中添加以下示例FeaturePermissionBar.qml。它与上面相同,只是添加了打印功能消息以及"接受"one_answers"拒绝"按钮。

import QtQuick 2.1
import QtQuick.Controls 1.0
import QtWebEngine 1.1
import QtQuick.Layouts 1.0
Rectangle {
property var requestedFeature;
property url securityOrigin;
property WebEngineView view;
id: permissionBar
visible: false
height: acceptButton.height + 4

function textForFeature(feature) {
switch (feature) {
case WebEngineView.Geolocation:              return 'Allow %1 to access your location information?'
case WebEngineView.MediaAudioCapture:        return 'Allow %1 to access your microphone?'
case WebEngineView.MediaVideoCapture:        return 'Allow %1 to access your webcam?'
case WebEngineView.MediaAudioVideoCapture:   return 'Allow %1 to access your microphone and webcam?'
case WebEngineView.DesktopVideoCapture:      return 'Allow %1 to capture video of your desktop?'
case WebEngineView.DesktopAudioVideoCapture: return 'Allow %1 to capture audio and video of your desktop?'
case WebEngineView.Notifications:            return 'Allow %1 to show notification on your desktop?'
default: break
}
return 'Grant permission for %1 to unknown or unsupported feature [' + feature + ']?'
}
onRequestedFeatureChanged: {
message.text = textForFeature(requestedFeature).arg(securityOrigin);
}
RowLayout {
anchors {
fill: permissionBar
leftMargin: 5
rightMargin: 5
}
Label {
id: message
Layout.fillWidth: true
}
Button {
id: acceptButton
text: "Accept"
Layout.alignment: Qt.AlignRight
onClicked: {
view.grantFeaturePermission(securityOrigin, requestedFeature, true);
permissionBar.visible = false;
}
}
Button {
text: "Deny"
Layout.alignment: Qt.AlignRight
onClicked: {
view.grantFeaturePermission(securityOrigin, requestedFeature, false);
permissionBar.visible = false
}
}
}
}

然后,对于main.qml,请尝试以下代码。这样,您就可以了解如何授予权限,并可以相应地将其应用于任何地方。

import QtQuick 2.13
import QtQuick.Window 2.13
import QtQml 2.0
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Layouts 1.0
import QtWebEngine 1.4
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
FeaturePermissionBar {
id: permBar
view: webEngineView
anchors {
left: parent.left
right: parent.right
top: parent.top
}
z: 3
}
WebEngineView {
id: webEngineView
anchors {
fill: parent
top: permBar.bottom
}
focus: true
url: "https://webcammictest.com/"
onFeaturePermissionRequested: {
permBar.securityOrigin = securityOrigin;
permBar.requestedFeature = feature;
permBar.visible = true;
}
}
}

最新更新