/

How to Play a Sound from the macOS Command Line

How to Play a Sound from the macOS Command Line

If you want to play a sound from the macOS command line, you can make use of the afplay command. Simply follow the steps below:

  1. Open the Terminal application on your Mac.
  2. Type the following command: afplay file.mp3
  3. Replace file.mp3 with the path to the sound file you want to play.

Additionally, here are some more options for the afplay command that you can experiment with:

  • Play a sound file and wait until playback ends:

    1
    afplay path/to/file
  • Play a sound file at 2x speed (adjust the playback rate):

    1
    afplay --rate 2 path/to/file
  • Play a sound file at half speed:

    1
    afplay --rate 0.5 path/to/file
  • Play only the first N seconds of a sound file:

    1
    afplay --time seconds path/to/file

If you prefer to have your Mac speak instead of playing a sound file, you can accomplish this using the say command. Here’s how you can do it:

  1. Open the Terminal application on your Mac.
  2. Type the following command: say "test"
  3. Replace "test" with the text you want your Mac to speak.

You can even customize the voice used by the say command. For example, if you want your Mac to speak with a voice named “Fred,” you can use the following command:

1
say -v Fred "Isn't it nice to have a computer that will talk to you?"

That’s it! Now you can easily play sounds or make your Mac talk from the macOS command line. Happy commanding!

tags: [“macOS”, “command line”, “afplay”, “say”]