【问题标题】:How to make QWebEngineView of PyQt5 able to play audio?如何让 PyQt5 的 QWebEngineView 能够播放音频?
【发布时间】:2017-05-13 09:04:04
【问题描述】:

我想在网络上使用 PyQt5 的 QWebEngineView 播放音频。这是我的代码:

import sys

from PyQt5 import QtWebEngineWidgets, QtWidgets

if __name__ == '__main__':
    app = QtWidgets.QApplication([])

    view = QtWebEngineWidgets.QWebEngineView()
    view.settings().setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
    view.settings().setAttribute(QtWebEngineWidgets.QWebEngineSettings.JavascriptEnabled, True)

    html = '''
    <html>
        <audio id="pron" src="http://static.sfdict.com/staticrep/dictaudio/A06/A0612000.mp3"></audio>
        <button onclick="document.getElementById('pron').play()">Play!</button>
    </html>'''
    view.setHtml(html)

    view.resize(250, 150)
    view.move(300, 300)
    view.show()

    sys.exit(app.exec_())

但是当我点击Play 按钮时,音频没有播放。我怎么了?

【问题讨论】:

  • “苹果!”在linux上对我来说很好。设置没有区别。如果您使用的是 Windows,您是否安装了所有必要的音频编解码器?尝试使用指向 wma 文件的链接进行测试。
  • 与@ekhumoro 意见相同
  • 我在测试您的示例时收到控制台警告,即 js: Uncaught (in promise) NotSupportedError: The element has no supported sources.

标签: python pyqt pyqt5 qwebengineview


【解决方案1】:

QWebEngineView doesn't support mp3 playback by default,至少在我测试过的 Win7 上。如果您将 mp3 url 更改为 ogg one(默认情况下通过 QWebEngineView 支持 ogg 格式),例如

https://upload.wikimedia.org/wikipedia/commons/5/5a/Nl-URL%27s.ogg

那么你的例子就可以了!

当我搜索网络时,我发现启用 mp3 播放的唯一方法是编译我们自己的 Qt 网络引擎,someone told me the way to do it,如下所示,

编译你自己的Qt(包括QtWebEngine),然后编译PyQt和 调用其configure.py时,使用--qmake将路径传递给 正确的 qmake 可执行文件。

如果有人对编译 Qt webengine 感兴趣,这些信息可能会有所帮助

How to compile Qt webengine (5.11) on Windows with proprietary codecs

Unable to get mp3 support with QtWebEngine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2012-03-14
    相关资源
    最近更新 更多