【问题标题】:Two different audio file playing on Left channel and right channel with pygame使用pygame在左声道和右声道播放两个不同的音频文件
【发布时间】:2018-06-03 22:26:02
【问题描述】:

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

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

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)

非常感谢任何帮助。

【问题讨论】:

  • 通道的索引是否为零?您可能需要Channel(0)Channel(1)
  • 我没听懂?
  • pygame.mixer.Channel(0).play(m,-1) pygame.mixer.Channel(1).play(n,-1) 给出了相同的结果。
  • The docs 说你必须通过左右声道的音量,例如:channel1.play(sound1), channel1.set_volume(1.0, 0.0) 关闭右扬声器,但这对我不起作用。如果我只传递一个数字,它就可以正常工作。也许这是一个错误。
  • @skrx 谢谢!这实际上似乎是我问题的答案。

标签: python audio pygame mixer


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多