Pygame cannot open sound file

import pygame, time from pygame.locals import * soundObj = pygame.mixer.Sound('beeps.wav') soundObj.play() time.sleep(1) # wait and let the sound play for 1 second soundObj.stop() 

and this causes this error:

 Traceback (most recent call last): File "C:/Users/Jauhar/Desktop/Python/sounds.py", line 4, in <module> soundObj = pygame.mixer.Sound('beeps.wav') pygame.error: Unable to open file 'beeps.wav' 

The beeps.wav file is saved in the same directory as the python file in which the code is located.

I donโ€™t understand why this will not work!

+4
source share
6 answers

You cannot use the pygames library unless you initialize the modules you use or all pygame.

  pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize pygame.init() #turn all of pygame on. 

do this before doing anything in pygame. I recommend it.

+4
source

Pygame (at least version 2.9) does not support 32-bit floating point WAP files. Transcode it to a signed 16-bit WAV (e.g. using Audacity).

+3
source

Try to register this information and you may find your problem.

 import os os.getcwd() # Log this line. soundObj = pygame.mixer.Sound('beeps.wav') 

This should tell you in which directory your application is located, trying to access your sound file. You will probably find it in the base of your catalog of games.

0
source

Having a similar problem, I found that the .wav file size is related to the problem. Creating a smaller WAV file allowed it to work without making significant changes to the problem.

0
source

The same program works for me if I create a display surface before playing sound.

  #! Python 3 ''' Making games with python chapter 2 program 5 ''' import pygame, sys, time from pygame.locals import* pygame.init() DISPLAYSURF = pygame.display.set_mode((400, 300)) pygame.display.set_caption("Sound!!") soundObj = pygame.mixer.Sound('badswap.wav') soundObj.play() time.sleep(1) #wait and let the sound play for X second soundObj.stop() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() 
0
source

Enter the sound file after pygame.init (). I had this problem, but after placing the audio file after that, it worked fine for me.

0
source

All Articles