I need to be able to play up to 8 mp3 files at the same time. Is this possible with the raspberry pi 3b+? They need to be started in python code.


Based on my answer here:

The test is done on a Pi Zero with a recent Raspbian, Python 3.6, vlc (along with approx. 150 other packages), and it's Python-bindings. It's using a USB soundcard and alsa.

Playing from Python worked straight forward:

import vlc
instance = vlc.Instance('--aout=alsa')
p = instance.media_player_new()
m = instance.media_new('something.mp3') 
vlc.libvlc_audio_set_volume(p, volume)  # volume 0..100

I was able to run four independent mp3's from a single Python process with a CPU load of about 87%. Starting a fifth player resulted in this error:

alsa audio output error: cannot estimate delay: Broken pipe

Given the more powerful CPU of the Pi 3B+ and its four cores I dare say it'll work. Use multiprocessing though to allow for the distribution of the load to more than one core.

Further reading:

  • I need the option of also playing each on a loop, so you have the command for that? What usb sound card are you using?
    – mScientist
    Jul 8 '19 at 21:15

I believe this is a simple issue of running the mpg123 command multiple times.

sudo apt-get install mpg123
man mpg123

If you want it in python

import subprocess
subprocess.run(['mpg123','<your options>'])

I have an idea, why not you use Python to play sound. Here is some code: Before the code, you will need this library installed in your project.

# Import the library, and have it installed in your project
import simpleaudio

# Variables
sound_1 = './sounds/correct.wav'
sound_2 = './sounds/correct.wav'

# Use this function in your code to play sound
def playsound(directory):
    play = simpleaudio.WaveObject.from_wave_file(directory)

# Play the sound and wait for the sound to stop playing before continuing with the code
def playsound_wait(directory):
    play = simpleaudio.WaveObject.from_wave_file(directory)

# Run the code below to play the sound (I used the sound_1 and sound_2 variable)

Hope this makes it simple

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.