How can I play a sound from the command line in Mac OS X?

39

8

I would like to play a short sound file from the command line in Mac OS X, independent of any audio player application, in order to provide notification that a long job has finished.

Kevin Reid

Posted 2011-06-16T19:27:41.270

Reputation: 2 854

Answers

48

There is a built-in tool: afplay <sound file>. The man page does not document all of its options, which can be found via afplay -h:

Usage:
afplay [option...] audio_file

Options: (may appear before or after arguments)
  {-v | --volume} VOLUME
    set the volume for playback of the file
  {-h | --help}
    print help
  { --leaks}
    run leaks analysis
  {-t | --time} TIME
    play for TIME seconds
  {-r | --rate} RATE
    play at playback rate
  {-q | --rQuality} QUALITY
    set the quality used for rate-scaled playback (default is 0 - low quality, 1 - high quality)
  {-d | --debug}
    debug print output

It will not play more than one audio file.

Kevin Reid

Posted 2011-06-16T19:27:41.270

Reputation: 2 854

1

afplay sometimes has this bug: http://superuser.com/questions/319174/error-audioqueuestart-failed-when-running-afplay-repeatedly . Are there any alternatives?

– tog22 – 2015-01-13T12:48:12.133

Careful with the -v option: a value of 1 seems to mean "100%"! – doctaphred – 2016-04-08T17:52:45.970

20

One time, when the power went off at work, knowing that my firewall would return to that last state (powered on) when the electricty came back on, I wrote a script in bash that used the say command to wake me up when the power came back on.

Kirk

Posted 2011-06-16T19:27:41.270

Reputation: 2 182

Interesting note: if you are remotely logged into a machine via ssh, say won't work unless you sudo it. (Much fun for making other people's computers talk to them.) – Daniel Griscom – 2016-10-29T17:59:53.100

13I love abusing the say command. – NReilingh – 2011-06-16T21:17:06.553

Indeed, say is relevant to this sort of problem and a good alternate solution. Have a vote! I was looking specifically for playing a short sound, though, as hearing a phrase would get tiresome for my use case. – Kevin Reid – 2011-06-18T04:58:13.080

11

Have you considered printf "\a\a\a" or echo -e "\a\a\a"?

blahdiblah

Posted 2011-06-16T19:27:41.270

Reputation: 3 125

For those that don't know this is the control sequence character for 'bell', which on most systems will make a 'bonking' sound – John Hunt – 2018-04-19T14:11:28.273