So you want to play some music on you Pi? Well there are plenty of ways of playing music and we are going to cover just 2 simple ways.

Play music straight from the command line (LXTerminal)

Step 1 – Open LXTerminal

First open up LXTerminal on your desktop.

open-lxterminal

 

Step 2. Plug in your speakers or headphones in to the Pi’s  Audio Port.

rpi-audio-port

 

Step 3. Download an mp3 file to play.

In LXTerminal, type the following command to go get an mp3 file from the internet to play.

pi@raspberrypi ~ $ wget http://www.freespecialeffects.co.uk/soundfx/household/bubbling_water_1.mp3

Here’s what the above line does:

wget – This is the name of a handy little program that goes and fetches things from the internet.

http://www.freespecialeffects.co.uk/soundfx/household/bubbling_water_1.mp3 – This is the web address of the mp3 file we are going to get.

 

Step 4. Play the mp3 file.

Now run the following command.

pi@raspberrypi ~ $ aplay bubbling_water_1.mp3

Here’s what the above lines do:

aplay   –   The is the name of the program we want to run.

bubbling_water_1.mp3   – This is the name of the mp3 file we want the program to play, and we are passing it to the program as a command line parameter

Playing music using python

Step 5. Install Pygame

Now you need to download and install Pygame, the python module that lets you build games within python.

pi@raspberrypi ~ $ sudo apt-get install python-pygame

Here’s what the above does:

Sudo ( Super user do – give yourself special permission because you are installing software)
apt-get ( this is a program called Aptitude that goes and fetches most Linux software from the internet for you)
install (when we have got the software we want to install it)
python-pygame (The name of the software)

For those who are interested most Linux software is stored online repositories that you can access for FREE!

Step 6. Open up the python shell

pi@raspberrypi ~ $ python

The python shell is a program that lets you test out little bits of python code. As your code gets bigger, you are better off saving you code in a text file, otherwise you would have to retype the code every time you want to run your program!

Step 7. Type a simple script to play your mp3 file

>>>import pygame

This imports the pygame module into python, giving you access to the module.

>>>song = "bubbling_water_1.mp3"

This creates a variable ‘file’ and and sets it’s value to the name of your mp3 file, so we can access it in a bit.

>>>pygame.init()

This initialises the pygame module, starting up the pygame engine and setting up some things in the back ground.

>>>pygame.mixer.init()

This initialises the pygame mixer ( A bit like turning on the CD player in a car, after you have started the engine)

>>>pygame.mixer.music.load(song)

This loads the ‘song’ variable we made before ( a bit like putting a CD in the player)

>>>pygame.mixer.music.play()

This tells pygame to play the song(like pressing play on the CD player)

Hopefully now if all goes well you should hear some sounds from your speakers/headphones!

If you are saving all your code in a file and running the script straight from the command line, then you will need to add the following 2 lines to the bottom of your script:

while pygame.mixer.music.get_busy(): 
pygame.time.Clock().tick(10)

Why?  Well….Pygame doesn’t wait for the whole song to finish playing before moving on to the next line of code, so it would just reach the end of your script, close python and then stop playing the song. All this would happen so quickly that the song probably won’t even play at all! The code above makes python wait until the song has finished before closing python.