class Music():
def __init__(self, source):
self.source = source
self.clock = pygame.time.Clock()
self.quit = False
pygame.mixer.init()
pygame.display.set_mode((200, 100))
pygame.mixer.music.load(self.source)
def play(self):
pygame.mixer.music.play(0)
pygame.mixer.music.set_endevent(pygame.USEREVENT)
self.clock.tick(10)
while not self.quit:
try:
pygame.event.poll()
self.clock.tick(10)
except KeyboardInterrupt:
pygame.mixer.music.stop()
exit()
music = Music("music.mp3")
music.play()
I'm new to Python. The code will run. However the music sometimes doesn't play.
1st run: Play
2nd run: Play
3rd run: No sound
4th run: No sound
5th run: Play
6th run: Play
7th run: Play
8th run: No sound
9th run: No sound
10th run: Play
Hope you got what I mean, it is not consistent, sometime it works, sometimes don't.
I remember there was one time that when I leave the no sound running, suddenly it got sounds. It's kind of weird.