-1

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?

Rui
  • 97
  • 1
  • 8
  • You say "without any error message". Does it mean that you tried to run the program from a shell or command prompt? – musicamante Jul 10 '23 at 05:10
  • No, actually, I run it in its windows location directly just with a "double click". – Rui Jul 10 '23 at 05:13
  • My comment implied: "if you didn't, what is the ouput?". – musicamante Jul 10 '23 at 05:21
  • try to add thes command to the pyinstaller pyinstaller -w --onedir --add-data Qt\bin\Qt5Multimedia.dll;. --runtime-hook windows_runas_administrator myplayer.py – Abdelrahman Khallaf Jul 10 '23 at 05:23
  • @AbdelrahmanKhallaf, I tried this, myplayer.exe was generated as before, but it still can not be run in windows, no prompt, no error message, when I clicked myplayer.exe. – Rui Jul 10 '23 at 06:22
  • @musicamante, myplayer.exe has already been generated by pyinstaller, the issue is, myplayer.exe can not be run in windows, I will try to find and check os debug log files later. – Rui Jul 10 '23 at 06:25
  • I suggest when you make the exe using Pyinstaller you do not hide the console and then run the exe using the cmd, so the error will be printed in the cmd – Abdelrahman Khallaf Jul 10 '23 at 06:34
  • Nice idea, here is the error information printed in cmd, 'could not load multimedia backend "" QtMultimedia is not currently supported on this platform or compiler.' Maybe it's pyinstaller issue. I found some sugestions for PyQt6 in github and stackoverflow, however, not the final solution. – Rui Jul 10 '23 at 07:49
  • @Rui I was obviously **not** talking about the pyinstaller output, but that of the compiled binary. Please, pay more attention to what's being asked to you. You need to open a command prompt and run the compiled executable from there. – musicamante Jul 10 '23 at 12:19
  • @musicamante, sorry, I am not sure if any of my expression misled you. Based on the suggestion of @abdelrahman Khallaf, I built an .exe file WITH cmd prompt (that's the function of Pyinstaller, without -w option), and the prompt ```could not load multimedia backend "" QtMultimedia is not currently supported on this platform or compiler.``` is what I got. In short, for this question, it is not issue with python code or algorithm, but the bug with pyinstaller when python code use QtMultimedia class. By the way, I nearly get the solution now, will summary and share later. – Rui Jul 11 '23 at 03:45

1 Answers1

0

Solution is, to update pyinstaller! (upgraded version is 5.13.0 now)

Here is a short review,

  1. At first, I built a target file "myplayer.exe" with "pyinstaller -w --onedir myplayer.py", but this exe can not be run in windows without any error message/prompt.

  2. Then, as guys in comment suggested, to try to build "myplayer.exe" with console (by "pyinstaller --onedir myplayer.py"). Then error message is printed in console,

       could not load multimedia backend "" 
       QtMultimedia is not currently supported on this platform or compiler.
    

    That means this is a lib dependency or compiler issue. I tried to modify local myplayer.spec file referring to a similar solution in stackoverflow,[https://stackoverflow.com/questions/74415173], however, it did not work for my case.

  3. Continue to check pyinstaller side, and I just found a similar issue posted before, https://github.com/pyinstaller/pyinstaller/issues/6489, a fix has been merged in https://github.com/pyinstaller/pyinstaller/pull/6496

  4. Updated pyinstaller with "pip install --upgrade pyinstaller" and tried to package again with "pyinstaller -w --onedir myplayer.py". It works now!

Rui
  • 97
  • 1
  • 8