2

I am trying to run the simple code, to play the wav sound with playsound, (I found this way of sound playing on some website):

from playsound import playsound 
playsound("Piano.wav")

and when I run it, i get error:

Traceback (most recent call last):
  File "C:/Users/user/AppData/Local/Programs/Python/Python38/ьгышсф.py", line 3, in <module>
    playsound("Piano.wav")
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 35, in _playsoundWin
    winCommand('open "' + sound + '" alias', alias)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 30, in winCommand
    '\n    ' + errorBuffer.value.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte

Same for mp3 files. And I am using Python 3.8

  • try the same code in Python 2 – Marat Nov 21 '19 at 16:57
  • Looks like the error message that's created by the OS contains some characters that aren't in utf-8. As a quick fix, try changing playsound.py line 30 from `'\n ' + errorBuffer.value.decode())` to `'\n ' + errorBuffer.value.decode(errors="ignore"))`. Then you should be able to see the actual error. – Kevin Nov 21 '19 at 17:03
  • Sorry @Kevin, I am beginner, how do I change this line, if its an error message, not the line created by me. – Ivan Elagin Nov 21 '19 at 17:06
  • 1
    Edit the file `C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py` – NineBerry Nov 21 '19 at 17:07
  • Changing the line is easy. Open the file `C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py` in Notepad or some other editor, and replace the line that's there with the one I've suggested. Then save and run your script again. – Kevin Nov 21 '19 at 17:08
  • Thank You @Kevin, I did what you said, and got another error. – Ivan Elagin Nov 21 '19 at 17:13
  • playsound.PlaysoundException: Error 275 for command: open "Piano.wav" alias playsound_0.3574764503291783 – Ivan Elagin Nov 21 '19 at 17:13

2 Answers2

1

The error you get (275 = MCIERR_FILE_NOT_FOUND) is that the system cannot find the sound file. You need to specify the complete path to the file, not only the file name:

playsound("C:\\Path\\To\\Piano.wav")

If the sound file is in the same directory or a subdirectory of where the script file is placed, see How to properly determine current script directory? for how to get the path to the script file at runtime.

NineBerry
  • 20,173
  • 3
  • 50
  • 78
0

You usually get this error when an non-UTF-8 character is in your path (like Ü, Ö...). Try to avoid having such characters in the path of your sound file.

Ryan Z
  • 1