简体   繁体   中英

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 (eg)

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM