python - Pygame 混音器一次只播放一种声音

标签 python pygame

这是我的代码:

pygame.mixer.init(frequency=22050,size=-16,channels=4)
sound1 = pygame.mixer.Sound('sound1.wav')
sound2 = pygame.mixer.Sound('sound2.wav')
chan1 = pygame.mixer.find_channel()
chan2 = pygame.mixer.find_channel()
chan1.queue(sound1)
chan2.queue(sound2)
time.sleep(10)

我认为它会同时播放 sound1sound2(queue 是非阻塞的,代码会立即进入休眠状态)。< br/> 相反,它会播放 sound1,然后在 sound1 结束时播放 sound2

我已确认这两个 channel 在内存中是不同的对象,因此 find_channel 不会返回相同的 channel 。是不是我遗漏了什么或者 pygame 没有处理这个问题?

最佳答案

参见 Pygame Docs , 它说:

Channel.queue - queue a Sound object to follow the current

因此,即使您的轨道在不同的 channel 上播放,如果您强制播放每个声音,它们也会同时播放。

以及播放多种声音:

  • 打开所有声音文件,并将 mixer.Sound 对象添加到列表中。
  • 循环遍历列表,并开始播放所有声音......使用 sound.play

这会强制所有声音同时播放。
另外,请确保您有足够的空 channel 来播放所有声音,否则,某些或其他声音会被打断。
所以在代码中:

sound_files = [...] # your files
sounds = [pygame.mixer.Sound(f) for f in sound_files]
for s in sounds:
    s.play()

您还可以为每个声音创建一个新的 Channel 或使用 find_channel()..

sound_files = [...] # your files
sounds = [pygame.mixer.Sound(f) for f in sound_files]
for s in sounds:
    pygame.mixer.find_channel().play(s)

关于python - Pygame 混音器一次只播放一种声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15385727/

相关文章:

python - 如何从文件类型获取文件扩展名?

python - 我无法正确跟踪 PyGame 中的 KEYDOWN 事件

python - 将 Pygame 与 PyPy 结合使用

python - 随机砖颜色 -> TypeError : 'pygame.Color' object is not callable

python - .so 文件的无效 elf header 错误

python - while 真正的系统资源效果

python - 如何关闭 SQLAlchemy session ?

python - 键入提示: Argument Of Type Class

python - Pygame 的 colliderect 没有注意到碰撞

Python Pygame 新手代码显示空白屏幕