Command Line MP3 Player in Mac OS X
You don’t need to use iTunes to play music on your Mac. Mac OS X comes with an included command line audio player that you can use to play most audio files with. In this example we’re going to use an MP3 file but you can also play AIFF, WAV, m4a, and more through afplay or open commands.
For afplay, launch the Terminal and type the following command:
Obviously you need to replace audiofile.mp3 with your filename and path to that file. For reference, the path to your iTunes folder is: ~/Music/iTunes/iTunes\ Music/ and then you can use tab completion to select artist names quickly.
For iTunes, you can also use the ‘open’ command to initiate iTunes streams in iTunes itself, or whatever your default audio player app is:
This launches the mp3 within the actual GUI app associated with the filetype (mp3, in this case).
Whichever method, once the command is executed the audio file will start to play immediately, you can stop the audio from playing at any time by hitting Control+C within the Terminal window.
You can also use the afplay command to play an audio file in the background simply by appending an ampersand to the end of the command, as shown:
afplay audiofile.mp3 &
To launch iTunes in the background from the ‘open’ command, use the same ampersand:
open /path/to/mp3file.mp3 &
Now the song will play within the background and does not require the Terminal window to stay open.
The audio file will play until it’s over, otherwise you can type:
The first kills afplay, the second stops iTunes by force quitting.
That will stop the audio immediately.
In the past I have combined this with the text to speech ‘say’ command to play some amusing pranks on friends.