A player controlling omxplayer by gpio (/buttons) or keyboard keys.
This python program has been written as an example to have the Raspberry pi as mp3 player without LCD.
The program runs on a Raspberry Pi with OS Raspbian Jessie.
It is written in python. Tested with Python version 2 (2.7.9) as well as version 3 (3.4.2).
To run the program
python omxplayer-player DIRECTORY [FILE_EXTENSION]
where:
DIRECTORY is the directory (/folder) containing the files to play.
FILE_EXTENSION is optional and defaulted to 'mp3'.
To run it under Python3, just replace python with python3.
After a 'chmod +x omxplayer-player.py' you can also run the program straight ahead
omxplayer-player ARGUMENTS
As OMXPlayer is able to play video, the playing is not only limitted to .mp3 files.
Currently hardwired 3 buttons (Play, Previous and Next) and 1 LED (Play/Paused).
The player starts playing the first file on the list.
Pressing the Play button pauses the playing. Again pressing the Play button, the playing is resumed.
When the player is playing the LED is on. During the paused state the LED is blinking.
The Next button stops the current one being played and the player continuous to play the next one on the list.
The Previous button stops the current one being played, and the player continouous with the previous one on the list.
Playing of the files is continuously: when reaching the end of the list, it continuous with the first one. Therefore the sequence is : first => ... last => first ... etc. Pressing the Previous button when the first file is the current one being played, the player continuous the playing of the last file.,
The main target is the controlling of omxplayer by the GPIO. However, it can also be controlled by the keyboard.
The keys and their functions:
- SPACE key, the play/pause function
- the 'p' key for the previous function
- the 'n' key for the next function
The program is terminated by the 'ESC' (Escape) or 'q' key.
To play all mp3 files in the folder '/home/pi/music':
python omxplayer-player /home/pi/music
(which is equal to
python omxplayer-player /home/pi/music mp3
as .mp3 file_extension is default)
To play all mkv video files in the folder '/home/pi/movies':
python omxplayer-player /home/pi/movies mkv
The playing of the audio/video file is done with OMXPlayer. OMXPlayer is the standard player on the Raspberry Pi. It is part of the distribution, and therefore it needs not to be installed.
The GPIO support is done with python module 'RPi.GPIO'.
Install RPi.GPIO with:
sudo apt-get install python-rpi.gpio
For usage with python3:
sudo apt-get install python3-rpi.gpio
With the following commands you can use the Raspbian Jessie GPIOs without the need to be root (superuser):
sudo groupadd gpio
sudo usermod -aG gpio USERNAME
sudo reboot
Currently there are 3 buttons and 1 led hardwired. (GPIOxx is the BROADCOM name, and pin# means pin number)
btnPlay => GPIO25 (= GPIO header pin# 22)
btnPrevious => GPIO23 (= GPIO header pin# 16)
btnNext => GPIO24 (= GPIO header pin# 18)
ledPlay => GPIO22 (= GPIO header pin# 15)
+3.3V (= GPIO header pin# 1)
GND (= GPIO header pin# 6)
+3.3V --->---| button |-----> GPIOx
GPIOxx --->-----| Anode LED Kathode |----[ resistor 330R ]----> GND
Change requests / improvements / comments are welcome and can be made through Github's Issue or Personal Message (PM), or direct to [email protected]
- V0.10 Initial version