1

I tried a very simple code of playing music using pygame, however the cmd launches and then shuts down immediately without giving any substantial output. Please find the code attached below.

from pygame import mixer

mixer.init()
mixer.music.load("elvis.mp3")
mixer.music.play()
YASH Vala
  • 13
  • 2

2 Answers2

0

In the pygame docs it says:

Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.

So try to convert the file to another format, like WAV or OGG.

You could also use the absolute path to the file: full/path/to/your/file/elvis.mp3

Also make sure that elvis.mp3 is in your project folder.

krmogi
  • 2,588
  • 1
  • 10
  • 26
0

You have to wait for the music to finish playing:

from pygame import mixer
import time

mixer.init()
mixer.music.load("elvis.mp3")
mixer.music.play()

while mixer.music.get_busy():
   time.sleep(0.01)

mixer.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174