You have several problems with the two examples provided.
First:
while True:
clock.tick(60)
is an infinite loop in any context, not just pygame
, and will never exit.
Next:
while True:
clock.tick(60)
time.sleep(10)
break
will just break
on its first time through the loop and is equivilent to
clock.tick(60)
time.sleep(10)
which is why it works fine for a 10
second song.
If you want to use pygame.mixer.Sound
you should do it like this, using Sound.get_length()
import pygame
import time
pygame.init()
song = pygame.mixer.Sound("my_song.ogg")
clock = pygame.time.Clock()
song.play()
time.sleep(song.get_length()+1) # wait the length of the sound with one additional second for a safe buffer
pygame.quit()
print "done"
exit()
pygame
recommends using mixer.music
for things like this:
import pygame
import time
pygame.init()
pygame.mixer.music.load("my_song.ogg")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
pygame.quit()
print "done"
exit()