How to make QWebEngineView of PyQt5 able to play audio?

1.7k views Asked by At

I want to play audio on the web use QWebEngineView of PyQt5. This is my code:

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_())

But when I click the Play button, the audio not play. What's wrong with me?

1

There are 1 answers

0
iMath On BEST ANSWER

QWebEngineView doesn't support mp3 playback by default , at least on Win7 as I've tested . If you change your mp3 url to a ogg one(ogg format is supported by default through QWebEngineView ), e.g.

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

then your example would work !

As I searched web, I found the only way to enable the mp3 playback is to compile our own Qt webengine, someone told me the way to do it as following ,

Compile your own Qt (including QtWebEngine), then compile PyQt and when calling its configure.py, use --qmake to pass the path to the correct qmake executable.

If anyone interests in compiling Qt webengine, these information may be helpful

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

Unable to get mp3 support with QtWebEngine