0

In my PySide app, I use the following bit of code to play a wav file:

media = Phonon.MediaObject()
audio = Phonon.AudioOutput(Phonon.MusicCategory)
Phonon.createPath(media, audio)
alarm_file = 'alarm_beep.wav'
f = QtCore.QFile(alarm_file)
if f.exists():
    source = Phonon.MediaSource(alarm_file)
    if source.type() != -1:              # -1 stands for invalid file
        media.setCurrentSource(source)
        media.play()
else:
    logger.debug('Alert media missing: %s' % alarm_file)

This works fine in Ubuntu when I run the Python script but when I compile the app to an exe with Pyinstaller for windows, the sound is not played.

I use the following pyinstaller command

pyinstaller  --onefile --add-data "alarm_beep.wav;." main.py

to attempt to add the media file, but it's to no avail.

The exception in the console is

WARNING: bool __cdecl Phonon::FactoryPrivate::createBackend(void) phonon backend plugin could not be loaded
             WARNING: bool __cdecl Phonon::FactoryPrivate::createBackend(void) phonon backend plugin could not be loaded
             WARNING: bool __cdecl Phonon::FactoryPrivate::createBackend(void) phonon backend plugin could not be loaded
             WARNING: bool __cdecl Phonon::FactoryPrivate::createBackend(void) phonon backend plugin could not be loaded
             WARNING: Phonon::createPath: Cannot connect  MediaObject ( no objectName ) to  AudioOutput ( no objectName ).

Alert media missing: alarm_beep.wav

So obviously it's as if the "alarm_beep.wav" doesn't exist.

Not sure why the add-data command isn't taking care of it?

fpghost
  • 2,834
  • 4
  • 32
  • 61

1 Answers1

1

Once the application in bundled, the external files will be saved in a temporary directory, which you will need to reference. See this post for a discussion on referencing these external files. In short, you will need to update the path to your resource file before referencing it:

#resource_path is the relative path to the resource file, which changes when built for an executable
def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath('.')
    return os.path.join(base_path, relative_path)

and in the body of your code:

alarm_file = resource_path('alarm_beep.wav')
apogalacticon
  • 709
  • 1
  • 9
  • 19
  • thanks and sorry abt the delay. It seems you fixed part of my problem, but the sound still doesn't play and the error is `Trying to open alarm file...C:\Users\salami\AppData\Local\Temp\_MEI26~1\alarm_beep.wav WARNING: bool __cdecl Phonon::FactoryPrivate::createBackend(void) phonon backend plugin could not be loaded` – fpghost Mar 08 '18 at 23:45
  • In other words, it's looking in the right place now, the file is present, but this phonon error (warning) persists and no sound plays. – fpghost Mar 08 '18 at 23:45