1

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Bob Ross
  • 38
  • 1
  • 6
  • 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 (https://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 '19 at 10:47
  • Just tried it, and it still gives the same error. – Bob Ross Jan 14 '19 at 11:47
  • Please edit your question to add the code using the absolute path and the exact error message then. – bruno desthuilliers Jan 14 '19 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 '19 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 '19 at 12:06
  • @brunodesthuilliers Not sure what you mean, could you please clarify a little? – Bob Ross Jan 14 '19 at 13:33
  • In your soundtest module, before the call to playsound, add this: `open(path to your file).close()`. If the file cannot be opened because the path is wrong or you don't have permissions on it, `open()` will raise a proper IOError, so you'll know that the issue is with either the pass or the permissions. Else, it's possibly an issue in playsound itself (in the _playsoundOSX function) so you'll have to debug it (I'm not using macos so I can't help here). – bruno desthuilliers Jan 14 '19 at 13:41
  • Doing so, it says "Errno 21: Is a directory: – Bob Ross Jan 14 '19 at 13:57
  • Please edit your question to add the edited code and the exact error message - and while you're at it, fix your previous edit (the one where you tried with the absolute path) because what you posted is not valid python (you need to post the very exact code you executed and the very exact error message, else all bets are off). – bruno desthuilliers Jan 14 '19 at 14:19

3 Answers3

6

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.

Raj
  • 173
  • 2
  • 8
3

This allows it to work...

s_musicfile = "/Users/xxxxxxxxxx/Desktop/play this file.mp3"

s_musicfile = s_musicfile.replace(" ", "%20")

playsound(s_musicfile)
Greenonline
  • 1,330
  • 8
  • 23
  • 31
0

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
Flair
  • 2,609
  • 1
  • 29
  • 41
mmoayed
  • 11
  • 1
  • 1
  • 5