How can I play an mp3 with pygame?

2019-01-09 01:01发布

问题:

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?

回答1:

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)


回答2:

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.



回答3:

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


回答4:

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)


回答5:

Try this,

#!/usr/bin/env python
import pygame
from Tkinter import *
file = 'Your MP3 here'
root = Tk()
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
root.mainloop()

you will need to install Tkinter, however it will hopefully work and you wont need to manually set the time. If this worked please let me know, I did test this on my computer but sometimes things just don't what to work. I will try to help you if I can.



回答6:

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


回答7:

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


标签: python pygame