In playsound python, it plays music from root directory (like C:/final.wav) but not when I put the file in a folder (like C:/a/final.wav)

Viewed 435

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.
1 Answers

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.

Related