0

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

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 1
    We need basic debugging details. Is this just a problem in conjunction with other code? If you have just code that plays music, and *nothing else whatsoever*, and just let it run - does it run the whole time? Does this issue only happen for a specific song? Or a specific audio file format? – Random Davis Aug 23 '22 at 16:51
  • If you put a time.sleep(4*60) after your above code, does the whole song play? You must be doing something else with the mixer, or it's possible your pygame event loop must be running and it's not. – RufusVS Aug 23 '22 at 17:26

2 Answers2

0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ethan m.t
  • 3
  • 6
0

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()
Jobo Fernandez
  • 905
  • 4
  • 13