问题描述
我正在构建一个从 Python 调用的简单 Web 应用程序.我正在使用下面的代码.以编程方式授予对 Cam & 的访问权限的最简单方法是什么?加载此页面时的麦克风?我只在网上找到了 C++ 示例,在 Python 代码中找不到这样做的方法.
I am building a simple web app called from Python. I am using the below code. What is the easiest way to programatically grant access to the Cam & Mic when this page is loaded? I have only found C++ examples on the web and cannot find a way to do this within Python code.
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
app = QApplication([])
view = QWebEngineView()
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
推荐答案
要授予权限,您必须使用 QWebEnginePage 的 ="noreferrer">setFeaturePermission 方法,但必须当视图发出 featurePermissionRequested 时要求您这样做信号,这将指示 url 和 feature.p>
To give permission you must use the setFeaturePermission method of QWebEnginePage, but you must do it when the view asks you to do so when it emits the featurePermissionRequested signal, this will indicate the url and the feature.
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QUrl
class WebEnginePage(QWebEnginePage):
def __init__(self, *args, **kwargs):
QWebEnginePage.__init__(self, *args, **kwargs)
self.featurePermissionRequested.connect(self.onFeaturePermissionRequested)
def onFeaturePermissionRequested(self, url, feature):
if feature in (QWebEnginePage.MediaAudioCapture,
QWebEnginePage.MediaVideoCapture,
QWebEnginePage.MediaAudioVideoCapture):
self.setFeaturePermission(url, feature, QWebEnginePage.PermissionGrantedByUser)
else:
self.setFeaturePermission(url, feature, QWebEnginePage.PermissionDeniedByUser)
app = QApplication([])
view = QWebEngineView()
page = WebEnginePage()
view.setPage(page)
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
这篇关于授予对 Cam & 的访问权限将 Python 用于 PyQt WebEngine 的麦克风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!