How can I play multiple sounds at the same time in

2019-07-26 08:07发布

I have the music that is always run in the background and some activities that would play sound when triggered. The music works fine.

pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.ogg'))

The problem I have is that when there are 2 or more activities triggering sounds, then only one would be played (not including the background music) and the rest are muted. Is there any solution to this?

标签: pygame
1条回答
淡お忘
2楼-- · 2019-07-26 08:57

you can add sounds to different channels using the mixer:

pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\gun_fire.wav'))
pygame.mixer.Channel(1).play(pygame.mixer.Sound('sound\enemy_hit.wav'))

Within each channel you can still only play one sound at a time, but you can group sounds into different channels if they would need to play at the same time.

You can add more channels like this:

pygame.mixer.set_num_channels(10)  # default is 8

A simple example. For the docs on Channels, go to:

https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Channel

查看更多
登录 后发表回答