I was wondering if it is possible to play a sound over a different sound using the pygame mixer. So for example I would have quiet background music playing, and then something happens and another sound played on top if it. Currently for the background music I use this:
pygame.mixer.init()
pygame.mixer.music.load("Audio Assets//bob.wav")
pygame.mixer.music.play()
But if I play another sound in a method the background music completely stops and doesn't start over again. Is it possible to play two sound on top of eachother?
Edit: I am now wondering how I could play a sound while a show and image in Tkinter, here is my code for showing the image:
def one():
pone = PhotoImage(file="Image Assets//DEAD.gif")
labelone = Label(root, image=pone)
labelone.image = pone
labelone.pack(pady=70)
labelone.after(2000, labelone.destroy)
As you can see the image gets shown and then destroyed after 2 seconds. How would I be able to play audio during those seconds using the channels and mixer?
EDIT 2: It was very simple, I just placed pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\gun_fire.wav'), maxtime=600)
at the end of the method and it plays as the image is shown.
In order to play sound effects over music (or sound effects over other sound effects), you can use Channels. for example:
# initialize
pygame.mixer.pre_init()
pygame.mixer.init()
pygame.init()
# start playing the background music
pygame.mixer.music.load(os.path.join(os.getcwd(), 'sound', 'main_theme.wav'))
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(loops=-1) # loop forever
Then later in the code, you can play sound effects through Channels:
# play a sound on channel 0 with a max time of 600 milliseconds
pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\gun_fire.wav'), maxtime=600)
# you can play a longer sound on another channel and they won't conflict
pygame.mixer.Channel(1).play(pygame.mixer.Sound("sound\death.wav"), maxtime=2000)
For more information on Channels
if you are looking to set the volume of an individual channel, you can use the set_volume() function:
channel.set_volume(0.5) # play at 50% volume