本文介绍了如何在 pygame 中同时播放多个声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有总是在后台运行的音乐和一些在触发时会播放声音的活动.音乐效果很好.

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'))

我遇到的问题是,当有 2 个或更多活动触发声音时,只会播放一个(不包括背景音乐),其余的则静音.有什么解决办法吗?

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?

推荐答案

您可以使用混音器将声音添加到不同的通道:

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

这篇关于如何在 pygame 中同时播放多个声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:00
查看更多