Two different audio file playing on Left channel and right channel with pygame

六眼飞鱼酱① 提交于 2021-01-27 13:44:52

问题


I have a code where I specified two different audio files in two different channels and plays simultaneously, but I need a way to make each file play on only one channel and the other on the other channel. For instance, two audio files playing simultaneously on two seperate channels, Right and Left. Such that an audio plays on right speaker and the other audio plays on left speaker.

I tried with the code below, but the audio is not mapping to any specific channel but are playing on both speakers.

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)

Any help is much appreciated.


回答1:


The docs say that you have to pass the volume of the left and right speaker to 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)

Also, if you don't want to manage the channels yourself, you can just use the channel that Sound.play() returns.

channel = sound0.play()
channel.set_volume(1.0, 0.0)


来源:https://stackoverflow.com/questions/47921638/two-different-audio-file-playing-on-left-channel-and-right-channel-with-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!