W
W
wolf-98302014-06-14 17:43:00
Python
wolf-9830, 2014-06-14 17:43:00

How to play audio(.mp3) in python?

Hello! Which module can play music in mp3 format? Need cross platform. I tried PyQt Phonon, but it says that there is no module, although I installed it, I tried QSound but it only plays wav, what do you recommend?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruben Harutyunov, 2014-06-14
@wolf-9830

pygame

import pygame
pygame.init()
song = pygame.mixer.Sound('file.mp3')
clock = pygame.time.Clock()
song.play()
while True:
    clock.tick(60)
pygame.quit()

Pyglet
import pyglet
song = pyglet.media.load('file.mp3')
song.play()
pyglet.app.run()

Gstreamer
import pygst
pygst.require('0.10')
import gst
import gobject
import os

mainloop = gobject.MainLoop()
pl = gst.element_factory_make("playbin", "player")
pl.set_property('uri','file://'+os.path.abspath('file.mp3'))
pl.set_state(gst.STATE_PLAYING)
mainloop.run()

You can also try PyMedia and PyAudio

Y
yttrium, 2014-06-14
@yttrium

convert to OGG use PyVorbis, because the MP3 decoder is not so free anymore

R
ROMANGOin, 2021-10-04
@ROMANGOin

What if the music file is not seen by pygame?
Is this possible on pygame?:
import pygame
pygame.init()
powerup = pygame.mixer.Sound('Jeremy Blake - Powerup!.mp3')
clock = pygame.time.Clock()
def song(song):
pygame.mixer .music.stop()
song.play()
song(powerup)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question