Command Line MP3 Player in Mac OS X

Dec 7, 2010 - 9 Comments

terminal You don’t need to use iTunes to play music on your Mac, particularly if you just want to play an audio document. 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 as the audio, but you can also play AIFF, WAV, m4a, and many more audio formats through afplay or open commands.

How to Play Mp3 Files at Command Line on Mac

For afplay, launch the Terminal (found in the /Applications/Utilities/) and type the following command:

afplay audiofile.mp3

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.

An example syntax for playing a file in the iTunes library using afplay looks like the following, with a full path to the file name:

afplay ~/Music/iTunes/iTunes\ Music/Grateful\ Dead/Ripple-live.mp3

The afplay command works with mp3 files, but also nearly any other audio file you point it at too.

If you want to launch a song into itunes from the command line, you can do that too. Let’s assume iTunes is the default music player, which it is unless you’ve changed it to something else. For iTunes, you can also use the ‘open’ command to initiate iTunes streams in iTunes itself, or whatever your default audio player app is:

open /path/to/mp3file.mp3

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.

How to Play Audio from Command Line in the Background

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:

killall afplay

or

killall iTunes

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.

.

Related articles:

Posted by: David Mendez in Command Line, Mac OS, Tips & Tricks

9 Comments

» Comments RSS Feed

  1. blu says:

    From your current directory:

    for i in *mp3; do if [ -e “$i” ]; then afplay “$i”; fi; done

    don’t know why but for some reason I wouldn’t want to pipe it into bash again.

    you can use some globbing to specify the songs you want.

  2. Jacob says:

    It’s a royal pain to get it to play a bunch of mp3’s though. To get it to play all files in the current directory (sequentially), I had to do this:

    ls *.mp3 | awk ‘{print “afplay \”” $0 “\””}’ | bash

    • Bla says:

      How about:

      afplay *.mp3

      • Bla says:

        Ah I just realized afplay can only take one file at a time.

        Then this would be better:

        # Recursive:
        find . -name \*.mp3 -exec afplay {} \;

        # Non-recursive:
        find *.mp3 -prune -type f -exec afplay {} \;

  3. Donald says:

    it actually works, i had a bit of difficulty at the beginning but yea finally managed to get it working !

  4. scstsut says:

    # afplay file.mp3 &
    # killall -STOP afplay
    # killall -CONT afplay
    (then use the up-arrow to choose a previous command to avoid having to type the pause/unpause commands (-STOP/-CONT))

  5. […] you just want a basic command line mp3 player, you could use afplay, but if that’s not enough for you, install ViTunes. The little VIM plugin gives you full […]

  6. Alberto says:

    Maybe it is faster to use quicklook?

  7. Parakeet says:

    this + ssh is fun

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site