I created a program for my little sister to learn math and now want to add sound. So looked up on how to add sound to my program and found the winsound
module. I wrote this code:
import winsound
winsound.PlaySound("victory.wav", winsound.SND_FILENAME)
stopper = input("Input something to stop the program!")
But for some reason it only plays the default windows sound. (Bliiiiiingg)
The file victory.wav
is located in the same folder as the python script. Then I saw someone else having the same problem as me and he said that adding the full path solved the problem. So I did that:
winsound.PlaySound(r"C:\Users\Nutzer\Desktop\gui\victory.wav", winsound.SND_FILENAME)
But it still didn't work. So I tried different methods of adding the path:
winsound.PlaySound("C:/Users/Nutzer/Desktop/gui/victory.wav", winsound.SND_FILENAME)
# and
winsound.PlaySound("C:\\Users\\Nutzer\\Desktop\\gui\\victory.wav", winsound.SND_FILENAME)
Still not working. Then I decided to switch modules, so I installed the playsound
module and wrote the following code:
import playsound
playsound.playsound("victory.wav",block=True)
It said that it could not play the file, which means that it found the file. We're making progress. So I changed the file extention to .mp3 and added the full path again:
playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)
Now it said something different:
Error 277 for command:
open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Fehler beim Starten von MCI.
Error 305 for command:
close "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Zusätzliche Zeichen nach einer Zeichenkette mit Anführungszeichen sind nicht erlaubt.
Failed to close the file: "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Traceback (most recent call last):
File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 3, in <module>
playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 277 for command:
open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Fehler beim Starten von MCI.
Translated it means "Error starting MCI".
Then I decided to install pygame
and use the mixer
libary:
import pygame.mixer as mixyy
mixyy.init()
mixyy.music.load("victory.mp3")
mixyy.music.play(loops=0)
input = input("Press enter to stop the program.")
Surprisingly, that actually worked. But it caused the compiled file size to go from 16MB to 70MB and the compiled file could not even open.
So I scrapped that idea and found a module called mp3play
, and copied the example code, and adjusted it for me:
import mp3play
filename = r'C:\Users\Nutzer\Desktop\gui\victory.mp3'
clip = mp3play.load(filename)
clip.play()
import time
time.sleep(min(30, clip.seconds()))
clip.stop()
Now it said this:
Traceback (most recent call last):
File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 1, in <module>
import mp3play
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\__init__.py", line 4, in <module>
from .windows import AudioClip as _PlatformSpecificAudioClip
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\windows.py", line 27
print 'Error %s for "%s": %s' % (str(err), txt, buf)
^
SyntaxError: invalid syntax
I don't see anything wrong with the syntax, as I literally copied the example from their documentation.
I just can't seem to get anything to work. Things I also tried:
- Using a different file.
- Using various filetypes.
- Reinstalling windows.
- Trying on linux.
- Restarting my computer.
- Trying on a different computer.
- Trying on mac.
- Using a different python interpreter.
- Using a different code visualizer (VSC, Pycharm, Idle).
Could somebody please tell me how to fix this?