0

I would like to play selected audio files through the built-in audio port on a Raspberry 3 that is running Raspbian Stretch Lite, from a python program. Unfortunately, none of the libraries or external applications that I have tried for this purpose work.

sudo pip install pygame

results in

Unable to run "sdl-config". Please make sure a development version of SDL is installed.

and

pip install sdl

fails with

Could not find a version that satisfies the requirement sdl (from versions: ) No matching distribution found for sdl

mplayer won’t install.

mpg321 comes closest, installing but then failing when used, with ALSA lib errors, and you can’t install a new version of alas-utils because

Could not find a version that satisfies the requirement alsa-utils (from versions: ) No matching distribution found for alsa-utils

What do people do to play music on this OS?

  • I installed pygame using the command: "$ sudo pip install pygame" without any problem. I got the message "Requirement also satisfied, ...", I am using Rpi3B+ stretch 9 (regular version, not lite version). – tlfong01 Jul 8 at 4:06
  • I also tried to import the pygame module in a python program and run the demo code of analog/digital clock and found everything OK ( pygame.org/project/994 ). I have not yet try any audio player though. – tlfong01 Jul 8 at 7:56
  • related (if not dupe): raspberrypi.stackexchange.com/q/94098/19949 – Ghanima Aug 24 at 9:58
2

I have just tested it with two favorite programs: omxplayer and mpv. I used a fresh flashed up to date Raspbian Buster Lite but it shouldn't make a difference to Raspbian Stretch Lite. Playing music with this programs works out of the box without an modifications.

For testing first I plugged in a simple earphone into the analog audio output plug of my Raspberry Pi 4B. Then downloaded a test mp3 file from Sample .mp3 download. I selected the smallest one:

rpi ~$ curl https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3 > file_example_MP3_700KB.mp3

Then installed omxplayer:

rpi ~$ sudo apt install omxplayer

and played the test file:

rpi ~$ omxplayer file_example_MP3_700KB.mp3

Same with mpv:

rpi ~$ sudo apt install mpv
rpi ~$ mpv file_example_MP3_700KB.mp3

I would prefer omxplayer because it is lighter than mpv and explicitly supports the hardware acceleration of the Raspberry Pi.

Now play it from python3:

rpi ~$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('/usr/bin/omxplayer /home/pi/file_example_MP3_700KB.mp3')

or playing it in the background:

>>> import subprocess
>>> subprocess.Popen(['/usr/bin/omxplayer', '/home/pi/file_example_MP3_700KB.mp3'])
exit()
rpi ~$    # exit from python3 but still playing

You may have a look at an omxplayer-wrapper. Look at google with python omxplayer-wrapper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.