3

I am trying to make an alarm clock for school project. I saw tutorials of the same and found out about playsound library. In the video it worked fine even when the path was not of root directory, but that is not the case with me.

this works:

from playsound import playsound  

playsound('C:/final.wav')

but this does not:

from playsound import playsound 
 
playsound('C:/a/final.wav')

similarly, D:/final.wav works but not D:/Download/final.wav

This is the error:

Exception has occurred: PlaysoundException

    Error 259 for command:
        play C:/a/final.wav wait
    The driver cannot recognize the specified command parameter.
  File "D:\vs code\testo.py", line 2, in <module>
    playsound('C:/a/final.wav')

I have tried:

  • putting // or \ instead of / in the path
  • making sure the permissions for the folders are right (they are same as root directories)
  • putting the audio file in same directory as the code and just typing playsound('final.wav'), and it works.
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Zappp
  • 31
  • 2
  • How do you run the code you've written (i.e. via the command-prompt or vscode itself)? – Novus Edge Aug 19 '21 at 07:21
  • Try using `c:\\a\\filename`, windows filename use escape slash, it could be issue – Santhosh Reddy Aug 19 '21 at 07:24
  • 2
    Try r'C:\a\final.wav'. Using `/` is not the Windows convention and as you have discovered, does not always work. `//` will not help. To use \ inside a string, make it a raw string `r"..."`. If you don't want to do that, you need to double all the backslashes in the string: \\ instead of \ . – BoarGules Aug 19 '21 at 07:27
  • Does this answer your question? [what to do with Cannot specify extra characters after a string enclosed in quotation marks error?](https://stackoverflow.com/questions/68545311/what-to-do-with-cannot-specify-extra-characters-after-a-string-enclosed-in-quota) – Tomerikoo Oct 03 '21 at 15:08

1 Answers1

0

This question is a known issue which has been described here

In my test environment (Python 3.9, Windows 11), none of the following commands work:

playsound.playsound('D:\\StarWars3.wav')

or

playsound.playsound(r'D:\StarWars3.wav')

This problem happens with the current version of playsound library, which can be resolved by installing a previous version:

pip uninstall playsound
pip install playsound==1.2.2

Also, if your program is intended to run in multiple system environments, perhaps using relative path is a better approach. For example, create a separate 'Audio' folder in the same directory of your Python script and store the audio file there, your code can be simplified to:

import playsound


playsound.playsound('Audio/StarWars3.wav')

Which works perfectly without need to concern much about writing the path correctly.

Lang Zhou
  • 53
  • 1
  • 3
  • 7
  • If you think this question has an answer somewhere else in this site - [flag it as duplicate](https://stackoverflow.com/help/privileges/flag-posts) instead of reposting an answer... – Tomerikoo Oct 03 '21 at 15:07