Today, when I used pygame to play music, I encountered a strange problem. With pygame. INIT (), I couldn't hear the sound,
Remove the record and try again.CodeAs follows:
1. the following code can play normally and you can hear music
Import pygame, time
########## Pygame. INIT ()
Pygame. mixer. INIT ()
Pygame.mixer.music.load('music.ogg ')
Pygame. mixer. Music. Play ()
While pygame. mixer. Music. get_busy ():
Time. Sleep (0.1)
2. Add pygame. INIT () and run the code without sound.
Import pygame, time
Pygame. INIT ()
Pygame. mixer. INIT ()
Pygame.mixer.music.load('music.ogg ')
Pygame. mixer. Music. Play ()
While pygame. mixer. Music. get_busy ():
Time. Sleep (0.1)
3. No sound after running only pygame. INIT ()
Import pygame, time
Pygame. INIT ()
########## Pygame. mixer. INIT ()
Pygame.mixer.music.load('music.ogg ')
Pygame. mixer. Music. Play ()
While pygame. mixer. Music. get_busy ():
Time. Sleep (0.1)
4. Only use mixer. INIT () and pygame. INIT () to run okimport pygame, time
comment ') pygame. mixer. Music. Play () while pygame. mixer. Music. get_busy (): Time. Sleep (0.1) [the end]