0

I created a virtual assistant which can search for things for me online, open specific websites, open applications like word, powerpoint, etc.. using Python 3.6 on Windows 10. I have successfully converted it into .exe file and it's running smoothly BUT it doesn't play sound clips I included with my code. I put the sounds on the same directory as my code before I converted it to exe. Instead of playing the sound, it was replaced by the sound of windows when you are trying to run an application as ADMINISTRATOR (i hope this does make sense). Is there any way I can fix this or can Pyinstaller mix my code with the audio? Thanks for any help!

I've never tried anything aside from re-converting it because removing the audio file from the directory won't make any sense.

this is the part of my code that plays sounds. if anyone of you wants/needs to see my entire code (it has 500 lines) just tell me and I will provide it.

    elif "toss a coin" in recog1.recognize_google(audio, language="en-US"):
        coin = ["HEADS", "TAILS"]
        engine.say("Tossing a coin...")
        engine.runAndWait()
        winsound.PlaySound("coin_toss.wav", winsound.SND_FILENAME)
        engine.say("The coin toss shows" + random.choice(coin))
        engine.runAndWait()

    elif "roll a die" in recog1.recognize_google(audio, language="en-US"):
        engine.say("Rolling a die")
        engine.runAndWait()
        winsound.PlaySound("roll_die", winsound.SND_FILENAME)
        engine.say("The die result shows " + str(random.randint(0, 6)))
        engine.runAndWait()

when I run this on my IDE (Pycharm) it works flawlessly but when I run it on the command line, it runs too but the audio doesn't work.

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
Virtual Dreamer
  • 43
  • 1
  • 12
  • when you run on command line (and it doesnt work), have you tried running the .py file (not just exe file made by pyinstaller)? You may need to set current working directory when the py file starts running. If it works with py, it should also work with exe generated by pyinstaller. – Deepak Garud Jul 30 '19 at 12:01
  • yes i did and it shows me the specific error. thank you – Virtual Dreamer Jul 30 '19 at 12:13

1 Answers1

2

PyInstaller won't do such a thing. You need to provide your sound files manually. First, you need to add your sound files to the output executable with add-data flag then create a function to load your files from the extracted path.

Remember that I'm loading sound files with resource_path function. Put song files next to your script file.

import os
import sys

def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)


...
elif "toss a coin" in recog1.recognize_google(audio, language="en-US"):
    coin = ["HEADS", "TAILS"]
    engine.say("Tossing a coin...")
    engine.runAndWait()
    winsound.PlaySound(resource_path("coin_toss.wav"), winsound.SND_FILENAME)
    engine.say("The coin toss shows" + random.choice(coin))
    engine.runAndWait()

elif "roll a die" in recog1.recognize_google(audio, language="en-US"):
    engine.say("Rolling a die")
    engine.runAndWait()
    winsound.PlaySound(resource_path("roll_die"), winsound.SND_FILENAME)
    engine.say("The die result shows " + str(random.randint(0, 6)))
    engine.runAndWait()
...

And generate your executable with:

pyinstaller -F --add-data "path/to/coin_toss.wav;." --add-data "path/to/roll_die;." script.py
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67