Python - 'Error: AudioFileOpen failed ('wht?')', play audio file on pong game

848 Views Asked by At

I'm making Pong Game with Python and wanna put an audio file in it.

but the file doesn't play with this error message

Error: AudioFileOpen failed ('wht?')

here is my part of code. (I imported os module on top.)

if ball.ycor() > 290:
    ball.sety(290)
    ball.dy *= -1
    os.system('afplay bounce.wav')

The audio file (bounce.wav) is in same directory with Pong game python file. I don't know what the problem is.

please help me, thank you.

2

There are 2 best solutions below

0
On BEST ANSWER

You need to pass the absolute path to the afplay. Try using this:

import os

dir_path = os.path.dirname(os.path.realpath(__file__))
sound_path = os.path.join(dir_path, 'bounce.wav')

if ball.ycor() > 290:
    ball.sety(290)
    ball.dy *= -1
    os.system('afplay "{}"'.format(sound_path))
0
On

Use the playsound module instead:

from playsound import playsound
playsound('afplay bounce.wav')