Home > front end >  Music stops reproducing suddenly
Music stops reproducing suddenly

Time:08-24

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).

CodePudding user response:

As many of the comments say, more information would be helpful. 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, eg

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 independant of the rest of your code which could fix your issue.

CodePudding user response:

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()
  • Related