4

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.

Ghanima
  • 15,855
  • 15
  • 61
  • 119
mScientist
  • 107
  • 1
  • 1
  • 6

3 Answers3

2

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>'])
nohupt
  • 21
  • 2
2

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') 
p.set_media(m)
p.play() 
p.pause() 
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:

Ghanima
  • 15,855
  • 15
  • 61
  • 119
  • 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 08 '19 at 21:15
0

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.play()


# 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)
    play.play().wait_done()


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

Hope this makes it simple

Macintosh Fan
  • 368
  • 2
  • 3
  • 14