Issue
I am building a Telegram bot installed on my Raspberry Pi that will perform multiple functions. One of those is to play/pause/stop a 10-hours mp3 file in the Raspberry. So what I need is to control the audio playback with telegram messages.
To simplify it, I can save some instructions into a .txt file, and another python (or shell) script will check for those and perform an action to the audio player.
I am able to reproduce a file on the Raspberry's headphone jack using omxplayer:
omxplayer -o local --no-keys /home/pi/Desktop/10hRock.mp3 &
But omxplayer
has no stop/pause commands. You can press p/s while the command is running whithout --no-keys
, but as far as I know python doesn't support this. I have tried os.system()
.
So my question is: is there a library that handles mp3 file playback, AND allows me to choose between the HDMI or the headphone jack output? (Or a simple way to make sure audio goes always through the jack)
If there's another way to handle this, I'll be glad to try it out!
Thanks in advance :D
Solution
I would suggest going with VLC
pip install python-vlc
And then simply:
import vlc
from time import sleep
p = vlc.MediaPlayer(audio_file_path)
p.play()
sleep(2)
p.pause()
sleep(2)
p.play()
sleep(2)
p.stop()
Concerning the change in audio output, its a bit more tricky, looking around, you can start with something along these lines:
instance = vlc.Instance()
mediaPlayer = instance.media_player_new()
mediaPlayer.set_mrl(audio_file_path)
device_list = instance.audio_output_enumerate_devices()
for dev in device_list:
print(dev)
And here on Windows I'm stuck, because I don't get any device ID and can't seem to set the output. Anyway, after that you can apply these methods to the player:
mediaPlayer.audio_output_set(device_name)
mediaPlayer.audio_output_device_set(device_name,device_ID)
checkout some topics on the VLC Forum
Answered By - Bastien Harkins Answer Checked By - Mary Flores (WPSolving Volunteer)