Building an MP3 player with the Raspberry Pi

Playing MP3s on the Raspberry Pi

You can play MP3 music on your Pi using the omxplayer command, find an MP3 file, then try something like this:

omxplayer example.mp3

But what about building a music player with real buttons to change tracks? 

Controlling the MP3 player using the Pibrella

You can hook up a Pibrella board so that you have a jukebox with no need for keyboard, mouse or monitor, just press buttons to shuffle and play the next song.

To do this you need to write a short python script to handle button presses from the Pibrella. I've tried using omxplayer to play the music but it doesn't work too well in this way (I ended up with many tracks playing at the same time!), so we need to install the more server friendly mpd and mpc instead:

First install the pibrella libraries, then mpd and mpc:
sudo apt-get install python-pip
sudo pip install pibrella
sudo apt-get install mpd mpc

Once installed edit mpd's config to tell it where your music is. Run this from the Terminal sudo nano /etc/mpd.conf then find the line with the setting for music_directory and set it to wherever you've stored your MP3s.

Once you've done this you need to restart the mpd service:
sudo service mpd restart

You can now test out mpc with the following commands:
mpc update
mpc ls
mpc ls | mpc add
mpc play
mpc next
mpc clear

Now try this program, don't forget to run as root otherwise Pibrella won't work:
   1 import os
   2 import pibrella
   4 # Show we're loaded
   5 pibrella.lights.pulse(0.2)
   7 def mpc(command):
   8     os.system('mpc %s' % command)
  10 def mpc_add_all_to_playlist():
  11     os.system('mpc ls | mpc add')
  13 def set_up_playlist():
  14     mpc('update')
  15     mpc('clear')
  16     mpc_add_all_to_playlist()
  17     mpc('random on')
  18     mpc('repeat on')
  20 def play_next_mp3(pin):
  21     pibrella.lights.fade(100, 0, 0.3)
  22     mpc('play')
  23     mpc('next')
  25 set_up_playlist()
  26 pibrella.button.released(play_next_mp3)
  27 pibrella.pause()

Make it work with no monitor or keyboard

You can get the Pi to start working as a jukebox each time you boot up, then you don't need the keyboard or mouse attached.

Edit root's crontab -- its list of tasks to run periodically:
sudo crontab -e

Add this line at the end -- this tells it to start up the jukebox each time the Pi boots.
@reboot /home/pi/path/to/your/script

More buttons

As you can see in the photo above, once I got the red button working I then added more buttons to add Pause, Random On/Off and Shutdown. You can see the code for this here: