0

I just want to play a simple MP3 file on Linux directly from the Python code.

I've looked at this and this question and tried the following libraries but all of them failed: audioplayer, Ipython.display.Audio, pydub, pygame.mixer, ossaudiodev, soundfile.

Errors that I saw often were:

  • ModuleNotFoundError: No module named 'gi'
  • Errors with ffmpeg
Joooeey
  • 3,394
  • 1
  • 35
  • 49
  • `ModuleNotFoundError: No module named 'gi'` sounds as if it you were simply missing a dependency -- for ubuntu / debian, check this out: [What would cause the “gi” module to be missing from Python?](https://askubuntu.com/questions/80448/what-would-cause-the-gi-module-to-be-missing-from-python) "Errors with ffmpeg" is way too vague, but I'd guess this may also have to do with wrong / missing dependencies. – He3lixxx Jun 11 '21 at 11:36

3 Answers3

0

pyglet is the only solution I found that can play MP3 on Linux:

import pyglet
sound = pyglet.media.load("/home/max/Desktop/sound.mp3")
sound.play()

Source

Joooeey
  • 3,394
  • 1
  • 35
  • 49
0

There is a bunch of ways!

Try the vlc python module?

import vlc

file = vlc.MediaPlayer("example.mp3")
file.play()

You can also stop it with file.stop()

Since mp3 is not supported by some of the 'better' audio module you may also wanna convert it to wav. You could play and convert with pydub

from pydub import AudioSegment

file = AudioSegment.from_mp3("example.mp3")
file.export("new.wav", format="wav")
puhbeige
  • 125
  • 3
0

MPV.io would be a good one. Lightweight media player that plays just about anything you throw at it. Python-MPV to access features.

Has methods for playlists, if you want to do an entire directory. They also include directions for multiple GUI's; GTK, QT, be easy enough to use filedialog from TK.

Doyousketch2
  • 2,060
  • 1
  • 11
  • 11