1
votes

I am just trying to do this really simple thing where I want to play two different sound files depending on a touch sensor. The problem that I currently have is that I cannot even play one of them without anything else, because 'the files cannot be opened'.

import pygame

pygame.mixer.pre_init(44100, -16, 12, 512)
pygame.init

slowbeat = pygame.mixer.Sound('/home/pi/gpio-music-box/samples/slowbeat.wav')
slowbeat.set_volume(.4);

fastbeat = pygame.mixer.Sound('/home/pi/gpio-music-box/samples/fastbeat.wav')
fastbeat.set_volume(.4);

slowbeat.play()

The traceback:

Traceback (most recent call last): File "/home/pi/a.py", line 11, in slowbeat = pygame.mixer.Sound('slowbeat.wav') pygame.error: Unable to open file 'slowbeat.wav'

1
Please post the complete error message (traceback). It could be a problem with the paths or filenames. Double-check if they are correct. Also, you forgot the parentheses behind pygame.init. - skrx
ohh thankss and here is the whole error message: - Lola Pfeifer
Traceback (most recent call last): File "/home/pi/a.py", line 11, in <module> slowbeat = pygame.mixer.Sound('slowbeat.wav') pygame.error: Unable to open file 'slowbeat.wav' - Lola Pfeifer
Adding the missing parentheses fixed it, right? You have to call this function to initialize all pygame modules, otherwise some things won't work correctly. You usually also have to open a pygame window in order to play sounds and music. If you don't want to open a window, you can just call pygame.mixer.init() before pygame.init(). - skrx
hmm maybe I am doing something wrong but the same error message still pops up :( - Lola Pfeifer

1 Answers

0
votes

Adding the missing parentheses behind pygame.init() should fix the problem (unless something is wrong with the path and filenames). You have to call this function to initialize all pygame modules, otherwise some things won't work correctly. You usually also have to open a pygame window in order to play sounds and music.

If you don't want to open a window, you can just call pygame.mixer.init() before or instead of pygame.init().