When working extensively in the macOS Terminal, launching the Apple Music app or QuickTime Player just to preview a short sound clip or play background focus noise is a heavy disruption to your workflow. Fortunately, macOS includes a highly efficient, lightweight command-line utility specifically designed for audio playback: afplay (Audio File Play). This built-in tool allows you to play nearly any audio format without ever opening a graphical interface.
Why Use afplay Instead of a Media Player?
The afplay command is not a replacement for a full music library application. Instead, it shines in automation and efficiency. Because it requires almost zero system resources to run, it is perfect for playing alert sounds at the end of long shell scripts, previewing raw audio files on headless Mac servers (like Mac minis used for build farms), or simply playing ambient noise without distraction.
Supported Audio Formats
Because afplay taps directly into the core macOS CoreAudio framework, it natively supports a massive array of file types. You can confidently play standard formats like MP3, WAV, and AAC (M4A), as well as lossless formats like AIFF and ALAC, without needing to install third-party decoders or libraries.
Basic Audio Playback
Using the command is incredibly straightforward. You simply invoke the command followed by the path to your audio file.
- Open the Terminal application on your Mac (found in Applications > Utilities).
- Type
afplayfollowed by a space. - Drag and drop the audio file from a Finder window directly into the Terminal to automatically paste the file path, or type the path manually.
- Press Enter.
For example, to play a song located in your Downloads folder, the command looks like this:
afplay ~/Downloads/background_track.mp3
The audio will begin playing immediately. The Terminal session will remain occupied (meaning you cannot type new commands) until the track finishes playing.
Stopping Playback Prematurely
If you started playing a five-minute song and want to stop it before it concludes, simply click on the active Terminal window and press Ctrl + C. This sends an interrupt signal, instantly halting the audio playback and returning your command prompt.
Running Audio in the Background
By default, afplay holds your Terminal session hostage until playback is complete. If you want to start a song and continue working in the same window, you must run the process in the background. You can do this by appending an ampersand (&) to the end of your command.
afplay ~/Downloads/background_track.mp3 &
The Terminal will output a process ID (e.g., [1] 45982) and immediately give you your prompt back while the music plays. To stop background playback, you can use the killall command:
killall afplay
Controlling Playback Speed and Time
The afplay utility includes a few advanced flags for scripting purposes.
If you want to play a track faster or slower, use the -q (quality) flag combined with a speed modifier. For instance, to play a track at 1.5x speed, use:
afplay -q 1 ~/Downloads/background_track.mp3
If you only want to play the first 10 seconds of a file (highly useful for previewing a directory of sound effects), use the -t flag:
afplay -t 10 ~/Downloads/background_track.mp3