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?

3
  • 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, 2019 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, 2019 at 7:56
  • related (if not dupe): raspberrypi.stackexchange.com/q/94098/19949
    – Ghanima
    Aug 24, 2019 at 9:58

2 Answers 2

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.

0

How to play audio in Python3 on RaspiOS Bullseye lite (I'd recommend to use the current version) - because VLC supports many audio formats, I use this library.

Install pulseaudio:

sudo apt install alsa-oss libasound2-plugins libavresample4 pulseaudio pulseaudio-utils

Start server:

pulseaudio -D

You can display your audio sinks (outputs) with:

pacmd list-sinks

Playing is possible with pacmd as well but only WAV:

pacmd play-file  <path> <sinkno>

You have to specify the full, absolute path for the file.

Troubleshooting for pulseaudio:

Stopping server:

systemctl --user stop pulseaudio.socket
systemctl --user stop pulseaudio.service

Starting server interactively for output to screen:

pulseaudio -v

VLC: Install necessary parts of VLC:

sudo apt install vlc-bin vlc-plugin-base

Install VLC library for Python:

sudo pip install python-vlc

In Python:

import vlc
p = vlc.MediaPlayer("<audiofile>")
p.play()

Volume can be got and set with:

vlc.libvlc_audio_get_volume(p)
vlc.libvlc_audio_set_volume(p,50)

I hope this answer is helpful.

Additional hint: If you plug in your USB device, this device should be the default for audio output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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