使用 Python 为 PyQt WebEngine 授予对摄像头和麦克风的访问权限
Grant access to Cam & Mic using Python for PyQt WebEngine
我正在构建一个从 Python 调用的简单网络应用程序。我正在使用下面的代码。加载此页面时,以编程方式授予对摄像头和麦克风的访问权限的最简单方法是什么?我只在网上找到了 C++ 示例,无法在 Python 代码中找到执行此操作的方法。
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_()
要授予权限,您必须使用 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_()
所以我发现 Raspberry Pi 上的 PyQt 不包括对 WebEngine 功能的支持。因此 PyQt 中的 WebEngineView class 无法在 Pi 上使用。 (我真的不明白为什么它在 Ubuntu 上运行良好但在 Raspbian 上运行不正常,但无论如何...)。
我开始使用 Qt 本身,但后来了解到您可以使用以下方法
os.system('chromium-browser --use-fake-ui-for-media-stream %s' % URL)
开始 Chrome 并预先授予对麦克风和摄像头的访问权限。
我正在构建一个从 Python 调用的简单网络应用程序。我正在使用下面的代码。加载此页面时,以编程方式授予对摄像头和麦克风的访问权限的最简单方法是什么?我只在网上找到了 C++ 示例,无法在 Python 代码中找到执行此操作的方法。
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_()
要授予权限,您必须使用 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_()
所以我发现 Raspberry Pi 上的 PyQt 不包括对 WebEngine 功能的支持。因此 PyQt 中的 WebEngineView class 无法在 Pi 上使用。 (我真的不明白为什么它在 Ubuntu 上运行良好但在 Raspbian 上运行不正常,但无论如何...)。
我开始使用 Qt 本身,但后来了解到您可以使用以下方法
os.system('chromium-browser --use-fake-ui-for-media-stream %s' % URL)
开始 Chrome 并预先授予对麦克风和摄像头的访问权限。