0

I'm learning Python by making small games, with Turtle. I want a background music playing. It's the only sound that doesn't work. When I add this sound, other sounds don't work also !

Here is what I get :

winsound.PlaySound("background.wav", winsound.SND_ASYNC)
RuntimeError: Failed to play sound

My code:

while True:
winsound.PlaySound("background.wav", winsound.SND_ASYNC)

wn.update()

# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
Anita
  • 21
  • 3

2 Answers2

0

You should play the sound in another thread so that the code will be executing at the same time.

import time
import winsound
from threading import Thread

def play_sound():
   winsound.PlaySound("./background.wav", winsound.SND_FILENAME|winsound.SND_ASYNC)

while True:
   thread = Thread(target=play_sound)
   thread.start()
   print ('hello')
   time.sleep(2)

I don't have experience with this module but hope you get the idea.

Priya
  • 723
  • 1
  • 5
  • 7
0

Just import playsound get the audio file you need and run it.

from playsound import playsound
playsound('audio.mp3') # The audio file you want to play.
SecretLloyd
  • 109
  • 1
  • 13