彼此播放音乐和声音效果(PyGame)

米克尔

我想知道是否可以使用pygame混音器以另一种声音播放声音。因此,例如,我将播放安静的背景音乐,然后发生某些事情,并且如果有其他声音在上面播放。目前,对于背景音乐,我使用以下方法:

pygame.mixer.init()
pygame.mixer.music.load("Audio Assets//bob.wav")
pygame.mixer.music.play()

但是,如果我使用某种方法播放另一种声音,背景音乐将完全停止并且不会重新开始。可以在彼此之上播放两个声音吗?

编辑:我现在想知道如何在Tkinter中播放节目和图像时播放声音,这是显示图像的代码:

def one():
    pone = PhotoImage(file="Image Assets//DEAD.gif")
    labelone = Label(root, image=pone)
    labelone.image = pone
    labelone.pack(pady=70)
    labelone.after(2000, labelone.destroy)

如您所见,图像被显示,然后在2秒钟后销毁。在那几秒钟内,如何使用通道和混音器播放音频?

编辑2:这很简单,我只是放置pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\gun_fire.wav'), maxtime=600)在方法的末尾,它按照图像显示的方式播放。

第四次冰人

为了在音乐上播放声音效果(或在其他声音效果上播放声音效果),可以使用通道。例如:

# initialize
pygame.mixer.pre_init()
pygame.mixer.init()
pygame.init()

# start playing the background music
pygame.mixer.music.load(os.path.join(os.getcwd(), 'sound', 'main_theme.wav'))
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(loops=-1)  # loop forever

然后在代码后面,您可以通过Channel播放声音效果:

# play a sound on channel 0 with a max time of 600 milliseconds
pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\gun_fire.wav'), maxtime=600)

# you can play a longer sound on another channel and they won't conflict
pygame.mixer.Channel(1).play(pygame.mixer.Sound("sound\death.wav"), maxtime=2000)

有关频道的更多信息

如果要设置单个通道的音量,则可以使用set_volume()函数:

channel.set_volume(0.5)  # play at 50% volume

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章