3

这是我的代码:

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)

我认为它会同时播放(非阻塞且代码立即进入睡眠状态)sound1。 相反,它会播放,然后在完成时播放。 sound2queue
sound1sound2sound1

我已经确认两个通道在内存中都是不同的对象,所以 find_channel 没有返回相同的通道。是否有我遗漏的东西或者 pygame 没有处理这个?

4

2 回答 2

4

See Pygame Docs, It says:

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

So, even though your tracks are playing on different channels, so if you force each sound to play, they will play simultaneously.

And for playing multiple sounds:

  • Open all sound files, and add the mixer.Sound object to a list.
  • The loop through the list, and start all the sounds.. using sound.play

This forces all the sounds to play simultaneously.
Also, make sure that you have enough empty channels to play all sounds, or else, some or the other sound sound will be interrupted.
So in code:

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

You could also create a new Channel or use find_channel() for each sound..

sound_files = [...] # your files
sounds = [pygame.mixer.Sound(f) for f in sound_files]
for s in sounds:
    pygame.mixer.find_channel().play(s)
于 2013-03-13T13:09:49.420 回答
3

我唯一能想到的是 chan1 和 chan2 是相同的,即使它们是不同的对象,它们也可以指向同一个通道。

在获得频道后立即尝试排队,这样您肯定会使用 find_channel() 获得不同的频道,因为 find_channel() 总是返回一个非繁忙频道。

尝试这个:

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()
chan1.queue(sound1)
chan2 = pygame.mixer.find_channel()
chan2.queue(sound2)
time.sleep(10)
于 2013-03-13T13:42:19.237 回答