afplay - macOS


Overview

afplay is a command-line utility on macOS used to play audio files from the terminal. It supports various audio formats such as AIFF, WAV, MP4, and MP3. afplay is primarily used for testing audio files, automated scripts for audio processing, and software development tasks that require audio playback.

Syntax

The basic syntax for the afplay command is as follows:

afplay [options] audio_file
  • audio_file: This is the path to the audio file you want to play.

Options/Flags

afplay includes several options that modify its behavior:

  • -v volume: Sets the playback volume for the audio file, where volume is a floating-point number from 0.0 (silent) to 1.0 (maximum volume). Default is 1.0.
  • -h: Displays a help message and exits.
  • -t time: Allows you to specify the duration in seconds to play the audio file. If the file is longer, playback will stop after the specified duration.
  • -r rate: Adjusts the playback rate of the audio file. A rate of 1.0 plays at normal speed, while greater than 1.0 increases speed, and less than 1.0 decreases it.

Examples

  1. Playing an audio file:

    afplay song.mp3
    

    This command plays the song.mp3 file at normal speed and volume.

  2. Playing an audio file at a lower volume:

    afplay -v 0.5 song.mp3
    

    This plays the song.mp3 file at half the maximum volume.

  3. Playing an audio file for a specific duration:

    afplay -t 10 song.mp3
    

    This plays the song.mp3 for the first 10 seconds of the file.

  4. Playing an audio file at a faster speed:

    afplay -r 1.5 song.mp3
    

    This plays the song.mp3 at 1.5 times the normal speed.

Common Issues

  • File not found: Ensure the path to the audio file is correct and that the file exists.
  • Unsupported audio format: While afplay supports many formats, some might not be compatible. Convert these to a supported format before playing.

Integration

afplay can be combined with other commands for more complex tasks. For instance, to play all MP3 files in a directory:

for file in *.mp3; do
    afplay "$file"
    sleep 1 # Pause for 1 second between files
done
  • audiodevice: A command-line utility to manage audio devices.
  • say: Converts text to audible speech.

For more detailed information, refer to macOS’s official documentation or use man afplay in the terminal to access the man pages.