Play MP3’s, M4A, AAC, and Other Music Files from the Command Line
Ever wanted to play music while you’re working in the command line? With the command line tool ‘afplay’ you can do just that, you can play practically any audio file format, whether it’s M4A, AAC, MP3, WAV, AIFF, or whatever else is on your Mac, and you can start the audio right from the Terminal. Just type:
You can then stop it with Control-C, otherwise the song will play itself through then terminate.
Don’t think of this as a terminal-based iTunes replacement (there’s a cool app called ViTunes for that though), because it’s not, it’s more of just a way to play a variety of audio files from the command line without having to enter into any GUI app. You won’t find any management utilities or syncing abilities within afplay.
That doesn’t mean you can’t run music in the background though. If you’d rather afplay run in the background while continuing to play the audio file, use this command:
afplay /path/to/audiofile.mp3 &
Note the ampersand is at the back of the command. This launches afplay into the background, if you want to shut it up you can just kill the afplay process with:
Or target the specific afplay process ID with kill -9:
Locate the PID for afplay as it’s running, then issue the command as follows:
kill -9 pid
That’ll end the process and music stream instantly.
Not that I’m condoning this type of behavior, but I recently used the afplay command to play a pretty amusing prank on a coworker by SSHing into his work machine and forcing him to listen to a Miley Cyrus song, he didn’t figure out what was going on until it was over and I told him. Yup, that’s our idea of fun here in nerdville.