两个不同的音频文件在左声道和右声道播放与pygame

问题描述 投票:2回答:1

我有一个代码,我在两个不同的通道中指定两个不同的音频文件并同时播放,但我需要一种方法使每个文件只在一个通道上播放而另一个在另一个通道上播放。例如,在两个单独的通道(右和左)上同时播放两个音频文件。使得音频在右扬声器上播放而其他音频在左扬声器上播放。

我尝试使用下面的代码,但音频没有映射到任何特定频道,但正在两个扬声器上播放。

pygame.mixer.init(frequency=44000, size=-16,channels=2, buffer=4096)
#pygame.mixer.set_num_channels(2)
m = pygame.mixer.Sound('tone.wav')
n = pygame.mixer.Sound('sound.wav')
pygame.mixer.Channel(1).play(m,-1)
pygame.mixer.Channel(2).play(n,-1)

任何帮助深表感谢。

python audio pygame mixer
1个回答
1
投票

文档说你必须将左右扬声器的音量传递给Channel.set_volume

# Create Sound and Channel instances.
sound0 = pg.mixer.Sound('my_sound.wav')
channel0 = pg.mixer.Channel(0)

# Play the sound (that will reset the volume to the default).
channel0.play(sound0)
# Now change the volume of the specific speakers.
# The first argument is the volume of the left speaker and
# the second argument is the volume of the right speaker.
channel0.set_volume(1.0, 0.0)

此外,如果您不想自己管理频道,可以使用Sound.play()返回的频道。

channel = sound0.play()
channel.set_volume(1.0, 0.0)
© www.soinside.com 2019 - 2024. All rights reserved.