How can I play an mp3 with pygame?

2019-01-09 01:32发布

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?

标签: python pygame
7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-09 01:41

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()
查看更多
Root(大扎)
3楼-- · 2019-01-09 01:42

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()
查看更多
Animai°情兽
4楼-- · 2019-01-09 01:44

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")
查看更多
相关推荐>>
5楼-- · 2019-01-09 01:46

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楼-- · 2019-01-09 01:48

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.

查看更多
劫难
7楼-- · 2019-01-09 01:58

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)
查看更多
登录 后发表回答