34
import pygame
file = 'some.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()

This outputs, "Process finished with exit code 0", but it doesn't play anything. How can I resolve this problem?

skrx
  • 19,980
  • 5
  • 34
  • 48
Ashot
  • 521
  • 2
  • 6
  • 15
  • did you try pygame.mixer.music.play(loops=-1) ??? – rocksportrocker Oct 12 '11 at 21:45
  • yes, i try change loops, set volume, change channels. Program running but no sound – Ashot Oct 13 '11 at 05:00
  • `while pygame.mixer.music.get_busy(): pygame.time.Clock().tick(10)` something like this resolve my problem. i don't know why but music gon busy. – Ashot Oct 13 '11 at 10:57
  • 1
    From http://pygame.org/docs/ref/music.html "Be aware that MP3 support is limited. [...] Consider using OGG instead." – 龚元程 Oct 14 '11 at 14:18
  • yes, but it's happened when i try make play sound `pygame.mixer.Sound(filename) Sound.play` – Ashot Oct 14 '11 at 19:02
  • My mp3s aren't playing either I had to convert to wav files which work fine. Very weird. – eric Jul 02 '17 at 01:17
  • Note that you need to display a screen like `pygame.display.set_mode((200,100))`, otherwise audio may be silient. – 576i Feb 18 '19 at 10:59

7 Answers7

31

The play function starts the music playing, but returns immediately. Then your program reaches it's end, and the pygame object is automatically destroyed which causes the music to stop.

As you commented, it does play the music if you wait for it before exiting - because then the pygame object isn't destroyed until the while loop finishes.

while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)
Ichigo Jam
  • 501
  • 3
  • 4
18

The music stops because it's an asyncronous event, which means it'll keep going with the script. then, the script stops instantly, not giving the music a chance to start. as stated before, you could use

while pygame.mixer.music.get_busy(): 
  pygame.time.Clock().tick(10)

however, even better is pygame.event.wait(), as it'll wait for all asynchronous events to end.

Cinder
  • 1,599
  • 2
  • 13
  • 23
16

Here is a super easy way.

import pygame
file = 'some.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
pygame.event.wait()
6

I've found a good solution from thepythongamebook.com:

pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag

pygame.init()

pygame.mixer.init()

pygame.mixer.music.load('music_01.mp3')

pygame.mixer.music.play(-1)
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Eric Menu
  • 61
  • 1
  • 1
2

try this one.

import pygame

def pmusic(file):
    pygame.init()
    pygame.mixer.init()
    clock = pygame.time.Clock()
    pygame.mixer.music.load(file)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        print("Playing...")
        clock.tick(1000)

def stopmusic():
    pygame.mixer.music.stop()


def getmixerargs():
    pygame.mixer.init()
    freq, size, chan = pygame.mixer.get_init()
    return freq, size, chan


def initMixer():
    BUFFER = 3072  # audio buffer size, number of samples since pygame 1.8.
    FREQ, SIZE, CHAN = getmixerargs()
    pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)

try:
    initMixer()
    file = 'C:\\data\\03.mp3'
    pmusic(file)
except KeyboardInterrupt:  # to stop playing, press "ctrl-c"
    stopmusic()
    print("\nPlay Stopped by user")
except Exception:
    print("unknown error")

print("Done")
Alfred George
  • 126
  • 1
  • 8
  • 4
    Welcome to StackOverflow. Answers with only code in them tend to get flagged for deletion as they are "low quality". Please read the help section on answering questions then consider adding some commentary to your Answer. – Graham Mar 06 '18 at 06:00
2

PyGame has 2 different modules for playing sound and music, the pygame.mixer module and the pygame.mixer.music module. This module contains classes for loading Sound objects and controlling playback. The difference is explained in the documentation:

The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.

If you want to play a mp3 file, you need to initialize the module. Load the file with pygame.mixer.music.load. Invoke pygame.mixer.music.play() to start playback of the music stream. Finally, you have to wait for the file to play.
Use pygame.mixer.music.get_busy() to test if a sound is being mixed. Query the status of the mixer continuously in a loop.
In the loop, you need to delay the time by either pygame.time.delay or pygame.time.Clock.tick. In addition, you need to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

import pygame
pygame.init()

pygame.mixer.music.load('my_music.mp3')
pygame.mixer.music.play()

clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
    clock.tick(60)
    pygame.event.poll()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

It seems the audio does not play because of the way you have imported it. The code below plays the sound as expected. Nothing has changed here except that rather than import pygame it uses from pygame import mixer. This may be due to the fact Pygame is a package but I'm not sure.

from pygame import mixer

file = 'some.mp3'
mixer.init()
mixer.music.load(file)
mixer.music.play()
Xantium
  • 11,201
  • 10
  • 62
  • 89