I just built an audio player with QMediaPlayer in Pyside6, and GUI/function works well when I run it with 'python3 myplayer.py' from console in windows(x64).
However, when I try to package it to be a windows executable file with pyinstaller by "pyinstaller -w --onedir myplayer.py", target .exe file is generated, but can not be run without any error message.
Here is demo code (can be run directly),
import os
import sys
import PySide6
from PySide6.QtCore import Qt,QUrl
from PySide6.QtWidgets import QApplication,QMainWindow,QWidget
from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtMultimedia import QSoundEffect, QMediaPlayer, QAudioOutput
from PySide6.QtWidgets import QPushButton
class PlugPlayer(QMainWindow):
def __init__(self, parent=None, flags=Qt.Widget):
super(PlugPlayer, self).__init__(parent)
self.main_widget = QWidget(self)
self.layout = QHBoxLayout(self.main_widget)
self.player = QMediaPlayer()
self.audioOutput = QAudioOutput()
self.player.setAudioOutput(self.audioOutput)
self.loadwavfile = r'D:\test\demo.wav'
self.player.setSource(QUrl.fromLocalFile(self.loadwavfile))
self.audioOutput.setVolume(20)
self.plbtn = QPushButton("Play")
self.stbtn = QPushButton("Stop")
self.pabtn = QPushButton("Pause")
self.layout.addWidget(self.plbtn)
self.layout.addWidget(self.stbtn)
self.layout.addWidget(self.pabtn)
#widget action
self.plbtn.clicked.connect(self.PlaySound)
self.stbtn.clicked.connect(self.StopSound)
self.pabtn.clicked.connect(self.PauseSound)
def PlaySound(self):
if self.loadwavfile is not None:
self.player.play()
def StopSound(self):
self.player.stop()
def PauseSound(self):
self.player.pause()
def main():
app = QApplication(sys.argv)
widget = PlugPlayer()
widget.show()
return app.exec()
if __name__ == '__main__':
main()
Is there something special should be set for QMediaPlayer when I build or call it as or in windows GUI exe?