hytromo
hytromo

Reputation: 1531

Pygame - Play sounds simultaneously

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.

Upvotes: 3

Views: 2101

Answers (2)

user1509818
user1509818

Reputation:

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.

Upvotes: 1

hammythepig
hammythepig

Reputation: 957

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.

Upvotes: 3

Related Questions