3
votes

I am making a Game using Pygame and what I'm trying to do is to have a main sound for every level and some default sounds being heard when collecting points (e.g.)

So, I load the main level music using:

pygame.mixer.music.load(music_file)
pygame.mixer.music.play(-1)

Now, what I want to do is to play a specific sound whenever a player collects a point. I cannot stop the music using:

pygame.mixer.music.stop()
pygame.mixer.music.load(point_music)
pygame.mixer.music.play()

because the level's music will stop playing.

So, I've tried doing something like this:

points_sound = pygame.mixer.Sound("point.mp3")
points_sound.play()

I know that sound playing in pygame runs on its own thread but I am sure that the program/game does not terminate prior to finish playing the sound.

Long story short: The player can collect points but I am unable to make pygame play the collecting points sound.

2
points_sound.play() looks good, Is there a problem with that soulution?pmoleri
Docs say mp3 support is limited for music and that only OGG and WAV are supported for Sound. Have you tried one of the formats that are officially supported?Silas Ray
sr2222 please give it as an answer so as to mark the thread as solved!hytromo

2 Answers

3
votes

As sr2222 said in comments:

Docs say mp3 support is limited for music and that only OGG and WAV are supported for Sound. Have you tried one of the formats that are officially supported?

Try out OGG or WAV formats for your sounds instead, that should work.

1
votes

points_sound.play() should return a channel object. This object is necessary for playing the sound.

points_channel = points_sound.play()

This has me helped in my own case.