CodePudding user response:
Music to play background music, if played the first music suggest using pygame. Mixer. The Sound objectCodePudding user response:
The import pygameFrom the tools import Color
WIN_W, WIN_H=400, 600,
Pygame. The init ()
The window=pygame. Display. Set_mode ((WIN_W WIN_H))
Window. The fill (Color. White)
Pygame. Display. Set_caption (' audio ')
Pygame. Display. The update ()
# 1. Music can only play a audio playback of audio - the same time (for background music play)
# 1) loading need audio
# the load (audio path) - in the player the specified audio
Pygame. Mixer. Music. The load (' resoures/bg. Mp3 ') # code directly paste here there will be a problem in the past, your debugging change the music path
# 2) broadcast audio
# play (cycles=0)
# cycles: 1 (infinite loop)
Pygame. Mixer. Music. Play (1)
# 3) set the volume
Volume # set_volume (value)
# the volume value is: the range of 0 ~ 1
Pygame. Mixer. Music. Set_volume (0.2)
# 4) suspend and resume (stop play can then next time after the end of the last position to continue to play)
# pygame. Mixer. Music. Pause ()
# pygame. Mixer. Music. Unpause ()
# 5) stop (can only start from scratch playing)
Pygame. Mixer. Music. The stop ()
# 2. As an object of a class can play multiple audio playback of audio - the same time, create multiple objects)
# 1) according to the audio path to create Sound object
Sound1=pygame. Mixer. Sound (' resoures/hi. Wav) # code directly paste here there will be a problem in the past, your debugging change the music path
Sound2=pygame. Mixer. Sound (' resoures/star. Wav) # code directly paste here there will be a problem in the past, your debugging change the music path
# 2) broadcast audio
# sound1. Play ()
Sound2. Play (1)
# 3) set the volume
Sound2. Set_volume (0.1)
# 4) stop
Sound2. Stop ()
While True:
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
The exit ()
The original code and comments and should be able to solve your problem ~