Pygame cannot open sound file
43,127
Solution 1
You cannot use pygames' library's unless you initialize either the modules your using or all of pygame.
pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize
pygame.init() #turn all of pygame on.
do these before you do anything in pygame. I recommend it.
Solution 2
Pygame (version 2.9 at least) doesn't support 32-bit float WAVs. Re-encode it to a signed 16-bit WAV (using Audacity for example).
Author by
user2066880
Updated on August 04, 2022Comments
-
user2066880 almost 2 years
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 it throws 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 the code is in.
I can't understand why it won't work!
-
user2066880 over 11 yearsHey thanks for your help. I modified the code to this:
import pygame, time, os from pygame.locals import * print(os.getcwd()) # Log this line soundObj = pygame.mixer.Sound('beeps.wav') soundObj.play() time.sleep(1) # wait and let the sound play for 1 second soundObj.stop()
You're right in that it's looking at the location at the base of my game directory. I'm not sure still how to fix my problem though as everything still seems to be working as intended. -
ninMonkey over 11 yearsYou need to init pygame. See kaliber's answer. If you need to use a subdir, use
filename = os.path.join("sounds", "beeps.wav")
-
Kaliber64 over 9 yearsIt may be a format or feature in the format it doesn't support. Pygames flexibility is rather limited and finding what works can be really constraining. I only use pygame now for events and an OpenGL window. If I ever do sound again I will probably use OpenAL.
-
Alex over 9 yearsIt turned out that the frequency rate was too high for pygame, so I used one of the free tools to reduce the sampling rate (to smth like 128 bps) and it worked.
-
ThePhi almost 9 years@Kaliber64 What's a 'too high frequency for pygame' exactly? Is the limitation really due to pygame or the computer CPU??
-
Kaliber64 over 8 years44100 is standard output quality. All Input from files will be constrained to this output. As long as the Pygame muxer(I think thats the right term) can handle the file you give it the sound will work. Unfortunately I have no idea on its limitations. Now all hardware less than 10 years old should not constrain what pygames sound library is capable of so don't worry about the computers CPU.
-
darthShadow about 7 yearsSome sample code to go along with your statement would be very helpful.
-
Devernay Stéphane over 4 yearsI confirmed. How is it possible ?
-
Athulya about 3 yearsWhat frequency should be set in pygame.mixer.pre_init(44100, 16, 2, 4096) if the audio file is at 16000Hz?
-
astralwolf almost 3 yearswav is more supported than ogg, try wav in the future if your OGG fails