Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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!

like image 870
user2066880 Avatar asked Feb 13 '13 03:02

user2066880


2 Answers

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).

like image 121
Getkey Avatar answered Sep 30 '22 15:09

Getkey


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.

like image 33
Kaliber64 Avatar answered Sep 30 '22 17:09

Kaliber64