Вы можете установить channels
для каждой песни, добавить песню на каждый канал, а затем управлять каналом вместо музыкального объекта.
Вот рабочий код. Он изменяет громкость по-разному для каждой песни, добавленной к каждому каналу. Программа принимает все песни в папке audio
в вашем текущем рабочем каталоге.
Программа упрощена для иллюстрации концепции. Вы можете, конечно, создать список песен и каналов, а затем добавить и обработать их на основе индекса.
Программа
import pygame
def checkifComplete(channel):
while channel.get_busy(): #Check if Channel is busy
pygame.time.wait(800) # wait in ms
channel.stop() #Stop channel
if __name__ == "__main__":
music_file1 = "sounds/audio1.wav"
music_file2 = "sounds/audio2.wav"
#set up the mixer
freq = 44100 # audio CD quality
bitsize = -16 # unsigned 16 bit
channels = 2 # 1 is mono, 2 is stereo
buffer = 2048 # number of samples (experiment to get right sound)
pygame.mixer.init(freq, bitsize, channels, buffer)
pygame.mixer.init() #Initialize Mixer
#Create sound object for each Audio
myAudio1 = pygame.mixer.Sound(music_file1)
myAudio2 = pygame.mixer.Sound(music_file2)
#Create a Channel for each Audio
myChannel1 = pygame.mixer.Channel(1)
myChannel2 = pygame.mixer.Channel(2)
#Add Audio to first channel
myAudio1.set_volume(0.8) # Reduce volume of first audio to 80%
print "Playing audio : ", music_file1
myChannel1.play(myAudio1)
checkifComplete(myChannel1) #Check if Audio1 complete
#Add Audio to second channel
myAudio2.set_volume(0.2) # Reduce volume of first audio to 20%
print "Playing audio : ", music_file2
myChannel2.play(myAudio2)
checkifComplete(myChannel2)
Программа выходного
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Playing audio : sounds/audio1.wav
Playing audio : sounds/audio2.wav
>>>
Fantastic! работает как очарование сейчас, спасибо :) – Reminent