Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

A while ago I made a little project, and I recently thought it would be cool to add some sounds to it as well. So I looked for ways to play sound in Python 3.x, and Playsound was well reviewed. I have it setup like so: I have folder Python Projects and inside that I have Sound Test But when I try to play my audio file ( test.wav ) it throws the following error:

Traceback (most recent call last):
  File "soundtest.py", line 2, in <module>
    playsound('test.wav')
  File "/Users/rhett/env/lib/python3.7/site-packages/playsound.py", line 67, in _playsoundOSX
    raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/rhett/Desktop/Python Projects/Sound Test/test.wav

I tried using the direct path, e.g.:

from playsound import playsound
playsound(/Users/Rhett/Desktop/Python\ Projects/Sound\ Test/test.wav)

I received the exact same error:

Traceback (most recent call last):
  File "Sound Test/soundtest.py", line 2, in <module>
    playsound("/Users/Rhett/Desktop/Python\ Projects/Sound\ Test/test.wav")
  File "/Users/rhett/env/lib/python3.7/site-packages/playsound.py", line 67, in _playsoundOSX
    raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/Rhett/Desktop/Python\ Projects/Sound\ Test/test.wav
                The fact that the file is in the same folder as your script is irrelevant - a relative path is resolved against the current working directory (en.wikipedia.org/wiki/Working_directory), not against your script's location. Try with an absolute path instead and see what happens.
– bruno desthuilliers
                Jan 14, 2019 at 10:47
                Please edit your question to add the code using the absolute path and the exact error message then.
– bruno desthuilliers
                Jan 14, 2019 at 11:52
                NB: added the macos tag because your issue is somehow macos specific (at least the fact you don't have a more explicit error message).
– bruno desthuilliers
                Jan 14, 2019 at 11:54
                Oh and yes: you can just try to open the file (and then close it...) in your "soundtest" module before calling playsound, so you will at least know if the issue is really an IOError and if yes which one.
– bruno desthuilliers
                Jan 14, 2019 at 12:06

I have found where the problem is. In the python3.7/site-packages/playsound.py file The developer has not checked for " " spaces in file path, so spaces character in the file path is creating trouble.

A quick fix without changing playsound.py 's code. replace your folder name ~/Desktop/Python Projects/Sound Test/test.wav with ~/Desktop/PythonProjects/SoundTest/test.wav

(i.e, remove spaces from folder names)

This will fix your error.

s_musicfile = "/Users/xxxxxxxxxx/Desktop/play this file.mp3"
s_musicfile = s_musicfile.replace(" ", "%20")
playsound(s_musicfile)

I think this will work try it especially when it is for raspberry :

Python 2:

sudo apt install python-gst-1.0

Python 3:

sudo apt install python3-gst-1.0
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.