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?
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.
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 ,
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