Play MP3’s, M4A, AAC, and Other Music Files from the Command Line

Oct 27, 2009 - 1 Comment

terminal-icon 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:

afplay /path/to/audiofile.mp3

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:

killall afplay

Or target the specific afplay process ID with kill -9:

ps|grep afplay

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, How to, Tips & Tricks

One Comment

» Comments RSS Feed

  1. Tony says:

    kill, instead of killall should do it, also, you can bring the program to the foreground with fg, then crtl-c

Leave a Reply


Shop for Apple & Mac Deals on

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Recent Posts