Music stops reproducing suddenly

71 Views Asked by At

I'm trying to make a game with pygame and then I would like to make it sound some music.

musica = pygame.mixer.Sound("song.mp3")
musica.play()

(I don't see necessary showing more code because nothing more interacts with the sound) The music plays and it's all good, but after like 20 seconds pass, the music stops for some reason (real song lasts almost 4 minutes).

2

There are 2 best solutions below

0
Ethan m.t On

However, a way to potentially get around this (assuming there is something else in your program that's accidentally interfering with it) is using threading. Import threading at the top of your program then define a function that plays the music, e.g.

import threading 

def musicplayer():
    musica = pygame.mixer.Sound("song.mp3")
    musica.play()

x = threading.Thread(target=musicplayer, args=(), daemon=True)

x.start()

This makes the music playing independent of the rest of your code which could fix your issue.

0
Jobo Fernandez On

If you are dealing with background music, perhaps you mean to use pygme.mixer.music.load()? Followed by pygame.mixer.music.play() to play the music? Or pass -1 as parameter to pygame.mixer.music.play(-1) if you want the music to loop?

pygame.mixer.music.load("song.mp3")
pygame.mixer.music.play()