1

I tried to use the playsound module to play an audio file. When I run my code I get this error:

Traceback (most recent call last):
  File "/Users/Tarantino/Desktop/CodeTesting/sound.py", line 3, in <module>
    playsound('sound.mp3')
  File "/Users/Tarantino/Library/Python/3.8/lib/python/site-packages/playsound.py", line 67, in _playsoundOSX
    raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/Tarantino/Desktop/Code Testing/sound.mp3

I have the audio file in the same folder as the Python code, could I please get help on what to do?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Can you share the actual code and the full error? Is `file:///Path/to/file` the actual argument you use in your code or is just you "censoring" the path? – Tomerikoo Sep 21 '20 at 10:23
  • Here is the full Error I get: Traceback (most recent call last): File "/Volumes/Σ/[MARVEL]/JarvisVoice.py", line 3, in playsound('sound.mp3') File "/Users/Tarantino/Library/Python/3.8/lib/python/site-packages/playsound.py", line 67, in _playsoundOSX raise IOError('Unable to load sound named: ' + sound) OSError: Unable to load sound named: file:///Volumes/Σ/[MARVEL]/sound.mp3 – illuminatro Sep 21 '20 at 10:25
  • My code: from playsound import playsound playsound('sound.mp3') – illuminatro Sep 21 '20 at 10:27
  • Also having special or non-ascii characters in path may be an issue too. – Arty Sep 21 '20 at 10:33
  • Did you already saw this? https://github.com/TaylorSMarks/playsound/issues/13 – adama Sep 21 '20 at 10:33
  • Agree with @Arty. Could you try to move the script and `.mp3` file to you home folder and retry? – heLomaN Sep 21 '20 at 10:36
  • @illuminatro please [edit] that into the question itself. It is very hard to read this way – Tomerikoo Sep 21 '20 at 10:39
  • Put the sound file and .py file in a new folder with ascii characters and no spaces but still get the error – illuminatro Sep 21 '20 at 10:44
  • @illuminatro What full path is printed at the end of exception message now? – Arty Sep 21 '20 at 11:12
  • @illuminatro Also try upgrading library through `python -m pip install --upgrade playsound`, I've just installed new version of library and it plays for me. – Arty Sep 21 '20 at 11:15

1 Answers1

1

Move file you're playing to folder that has no special or non-ascii characters on the full path from root of disk, spaces in files/dirs names are not allowed too. Because playsound is creating an URL from full path-location of file and this URL should has valid for URLs chars.

E.g. /x/y/z.mp3 path is alright, but /x y/z.mp3 and /x[y]/z.mp3 and /x/漢/z.mp3 are not.

Also try upgrading library via python -m pip install --upgrade playsound, I've just installed library and tried playing sound and it plays for me.

Also some dir on the path might not have read permissions for the user you are running python with.

Arty
  • 14,883
  • 6
  • 36
  • 69
  • 1
    The new path is in accordance to the change you suggested but still get the same error – illuminatro Sep 21 '20 at 11:05
  • @illuminatro Can you put here in comment new full path? The way that it has printed `playsound` in your Python exception? If not secret. I think you have spaces in folder/dirs names, they are not allowed. – Arty Sep 21 '20 at 11:06
  • Path for the file? IF so, here: **file:///Users/Tarantino/Desktop/CodeTesting/sound.mp3** – illuminatro Sep 21 '20 at 11:34
  • @illuminatro Did you also tried to upgrade library through `python -m pip install --upgrade playsound`? And checked dirs/file read permission for user running python? – Arty Sep 21 '20 at 11:52
  • You may do `cp sound.mp3 /tmp/` + `sudo chmod 777 /tmp/sound.mp3` and then play `/tmp/sound.mp3`. – Arty Sep 21 '20 at 11:53
  • That method worked to bring me to a shorter PATH but same error. Playsound is up to date too @Arty – illuminatro Sep 21 '20 at 12:04
  • @illuminatro Could it be the case that file `sound.mp3` is broken somehow? E.g. truncated or has bad data? Have you tried some other example `.mp3`? – Arty Sep 21 '20 at 12:09