我想在2个单独的实例中使用导入模块
[python]
import pygame
[...]
player1 = pygame.mixer
player1.init()
player1.music.load('file1.mp3')
player1.music.play()
[...]
player2 = pygame.mixer
player2.init()
player2.music.load('file2.mp3')
player2.music.play()
我想同时播放file1.mp3和file2.mp3,但是这种编码方式开始播放file1.mp3,然后开始播放file2.mp3(file1.mp3已停止) 我想同时玩两个而不是一个
答案 0 :(得分:1)
这是pygame使用.mp3格式文件来并行再现声音的一种限制。我在这里找到此信息:Pygame - Play sounds simultaneously
要获得结果,必须将格式从.mp3更改为.wav或.ogg
请参见以下示例:
import pygame
pygame.mixer.init()
sounds = [pygame.mixer.Sound('file1.wav'),
pygame.mixer.Sound('file2.wav')]
channels = [sound.play() for sound in sounds]
while any(channel.get_busy() for channel in channels):
print('Playing ...')
pygame.time.wait(100)
print('Finished')