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

Oct 27, 2009 - 3 Comments

Terminal in macOS can play any audio file from command line

Ever wanted to play music while you’re working in the command line? Maybe you want to play a podcast from the command line on Mac?

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.

How to Play Any Audio File from the Command Line

The afplay command is versatile and can play m4a, mp4, aac, aiff, wav, and many other audio file formats. To use it yourself, here is what you would want to do:

  1. Open the Terminal applicaiton found in /Applications/Utilities/
  2. Now type the following, using the path to the audio file you wish to play from the command line
  3. afplay /path/to/audiofile.mp3

  4. Audio begins playing immediate, to stop the audio from playing you can hit CONTROL + C at the command line

You can stop the audio any time with Control-C, otherwise the song will play itself through then terminate itself automatically when completed.

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.

How to Play Audio Files in the Background from Command Line

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


Related articles:

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


» Comments RSS Feed

  1. Gary says:

    Thank you so much. This satisfies my wish to play a sequence of MP3 files without (formerly iTunes) I’d like to explain that as an example of how powerful this tool is.

    If you tried to feed afplay multiple files, you probably saw the admonishment that you “may only specify one file to play.” To a UNIX geek, that’s just a suggestion to let venerable UNIX tools manage the files. For instance, if all of the files are in the same folder and have names ending in “.mp3,” you can change directory to that folder and play them all with the following command:

    find . -name “*.mp3” -exec afplay “{}” \;

    To give an idea of the quality of afplay, in geek terms, it executes asynchronously. That means that it doesn’t return until the song is done. Therefore, when you play a bunch with a command like the above, each song will play in its entirety before the next song begins, which is naturally what we want.

    With UNIX tools, you can do about anything you want. With some extra work, you can control the order in which the songs are played. With a sequence of cron commands, you could specify exactly when you want each song to play. You could play a number of songs, put the script to sleep for a while to let you get some snacks and then start up again after a period. So thanks again for such a useful suggestion!


  2. Chris says:

    When I try to play a song using
    # afplay path/to/song.m4a
    It acts like it takes the command but I can’t hear anything. Am I missing something I don’t use the first backslash since I’m in my Directory but I figured I’d hear something.

  3. 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 on and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks


iPhone / iPad



Shop on Amazon to help support this site