Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: Use a built-in command line audio player UNIX
OS X 10.5 includes a command line audio player (in /usr/bin) called afplay. This is very useful if you want to play a sound file from the command line, shell script, Automator action, etc. The /usr/bin directory is in your path by default, so you can just type afplay file.mp3 to play that file.

afplay makes use of Core Audio, so I think it can play any audio file that QuickTime supports (including mp3, aiff, wav, etc.). If any one is interested, Apple also provides the source code for this application with the Xcode developer tools. You'll find it in this folder: /Developer » Examples » CoreAudio » Services » AudioFileTools.

[robg adds: There's a very simple man page for afplay, which then tells you that help is available with afplay -h. There are a few interesting options, including the ability to play a defined (in time) segment of a file, and to play a file to a defined audio output device.]
    •    
  • Currently 2.75 / 5
  You rated: 1 / 5 (12 votes cast)
 
[52,161 views]  

10.5: Use a built-in command line audio player | 28 comments | Create New Account
Click here to return to the '10.5: Use a built-in command line audio player' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Use a built-in command line audio player
Authored by: mark hunte on Oct 03, '08 08:09:28AM

Cool, I have been Hijacking the alert sound with a script in 10.4, to run headless sound, but could not do this in 10.5 due to the alert sound time limit. Thanks for the heads up on this.



---
mh



[ Reply to This | # ]
Play Audio in Background?
Authored by: MacTipper on Oct 03, '08 09:03:57AM

Is there a way to play the audio without having to keep the terminal window open?

On a related note, could I run it off an applescript and have the applescript quit without the audio stopping?

Thanks,
MacTipper



[ Reply to This | # ]
Play Audio in Background?
Authored by: macavenger on Oct 03, '08 09:26:28AM

Just add an & after the command and it will run in the background, thus allowing you to close the terminal window (or even quit terminal entirely) without affecting playback.

---
Aluminum iMac 20" 2.4 GHz/3GB/300GB HD



[ Reply to This | # ]
Play Audio in Background?
Authored by: MacTipper on Oct 03, '08 09:35:28AM

That only somewhat works. I can continue working in that same terminal window, however, if I close it, I get a warning saying that I'm going to terminate running processes (login, bash, afplay)

My command:

afplay /Users/Shared/iTunes/iTunes\ Music/Aaron\ Shust/Whispered\ and\ Shouted/01\ Long\ Live\ the\ King.mp3 &



[ Reply to This | # ]
Play Audio in Background?
Authored by: VxJasonxV on Oct 03, '08 09:56:58AM

Don't worry that you're doing something wrong. You're correct. Using & puts it in the background of the shell, but if you kill the shell, you kill that application as well.

(Applications don't run without a controller of some manner.)

You have two options, and both may be a bit more involved than you want to be.

One is that you can use the 'nohup' command to continue command execution even if the shell goes away. Or you can learn how to use the 'screen' command.

nohup will continue command execution until completion (or failure, either way a program exit), and then it'll go away.
screen is a "terminal multiplexer", it allows you to turn one terminal into many all self contained. It serves an excellent second purpose that if you're doing something (say, ssh'ed into a remote server) and your connection goes away, everything you were doing inside screen continues to run, or everything you have done remains there in context until you reconnect to the server and resume screen.
This of course works locally. Be forewarned this is a VERY extreme example. If you're running screen, and terminal crashes, nothing happens to screen. It's all still there to reconnect to when you re-open Terminal and reconnect to it.
The only thing screen doesn't do is survive a reboot (go figure).

It'll take a little bit of learning to get used to it (the man page is VERY big), but I absolutely swear by screen.



[ Reply to This | # ]
Play Audio in Background?
Authored by: rcbarnes on May 12, '12 04:23:45AM

There's an easier way to close a terminal window (which is not the preferred way to end the shell inside it---similar to using 'force quit' to end safari instead of the quit menu command) without OS X whining or killing the programs you've started:

Instead of ending a command with '&', end it with '& disown' (no quotes, of course).

Note: This will work for essentially *any* program, but if there are semi-colons (;) in the line you typed, you are running more than one program and simply adding ' & disown' to the end of the line may not be sufficient to get the behavior you want.

I am sure the OP gave up on this page long ago, but in case future readers who stumble on this page are daunted by your suggestions (valid though they are), I though this very simple fix would be useful.



[ Reply to This | # ]
Play Audio in Background?
Authored by: corienti on Oct 03, '08 02:45:25PM

This is a little complicated - the full details are far too involved to go into right here.
However, the bottom line is that you can control whether a shell's subprocesses (bash = your shell) are terminated, when the shell is terminated.

By default, I don't think bash itself terminates its backgrounded children, when you exit bash (by typing control-D or exit).
That is, it does not terminate its children on my system, but I've long forgotten if that was something I configured myself, or the default behavior. I think it's default behaviour though - pretty sure.

However, from just testing now, it appears that if you click the red "close" button, Terminal itself terminates bash AND its children for you.

So the simple solution might be to close your shell by typing "exit" or control-D instead of clicking the close button.

What actually happens when you click the red "close" button is that the system sends a TERM (exit) signal to the shell - and when bash receives a TERM signal, it sends a TERM to all of its children before it exits itself.
However when you terminate bash by typing "exit" or control-D, it doesn't send a TERM to its children.

nohup and screen are not needed.
ksh also does not terminate its children by default when exiting, and nohup is needed.
Whether bash does or not is definitely configurable.

There's a lot I'm not explaining here but as you can see it would run into pages and pages and pages if I tried to explain it all here now!

If you read a few man pages (particularly of bash) and do a bit of testing you should be able to get it to do what you want.



[ Reply to This | # ]
Play Audio in Background?
Authored by: yogiken on Aug 05, '10 11:04:21AM
There is a simpler way to run a program in the background and detach it from the controlling terminal without using nohup. Just do this.

(afplay song.mp3&)

The parentheses request the creation of a subshell which becomes the parent process for the program you run. You can verify this works as follows.


(sleep 60&)
ps aux|grep sleep

You'll see something like this.


you      44389   0.0  0.0  2426564    324 s001  SN   11:00AM   0:00.00 sleep 60
you      44395   0.0  0.0  2425712    280 s001  R+   11:00AM   0:00.00 grep sleep

(Ignore the grep sleep which is the command you just ran.) A minute later, the same ps/grep will reveal that the program has terminated (and so has the parent subshell).

[ Reply to This | # ]

Play Audio in Background?
Authored by: mark hunte on Oct 03, '08 09:47:21AM

That does not work

---
mh



[ Reply to This | # ]
Play Audio in Background?
Authored by: macavenger on Oct 04, '08 11:29:18AM

Does for me, exactly as I described. If it doesn't for you, then you are doing something different. I started the music with the command as I described, and the proceeded to completely quit terminal. The music happily continued playing. It works.

---
Aluminum iMac 20" 2.4 GHz/3GB/300GB HD



[ Reply to This | # ]
Play Audio in Background?
Authored by: mark hunte on Oct 05, '08 09:53:50AM
No I am not doing anything different? .

But what does work is,

afplay /Users/User/Music/MusicFile.mp3 &  exit

I can then close the window or quit terminal.

Thanks

---
mh

[ Reply to This | # ]

Play Audio in Background?
Authored by: tommybarbour on Oct 03, '08 10:00:12AM

it does work. when i run it, i get the following on my screen:

bash-3.2$ 2008-10-03 12:57:02.956 afplay[524:10b] Error loading /Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder: dlopen(/Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder, 262): no suitable image found. Did find:
/Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder: mach-o, but wrong architecture
2008-10-03 12:57:02.958 afplay[524:10b] Error loading /Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder: dlopen(/Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder, 262): no suitable image found. Did find:
/Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder: mach-o, but wrong architecture
2008-10-03 12:57:02.960 afplay[524:10b] Error loading /Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder: dlopen(/Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder, 262): no suitable image found. Did find:
/Library/QuickTime/DivX 6 Decoder.component/Contents/MacOS/DivX 6 Decoder: mach-o, but wrong architecture


hit enter to return to your prompt.
type 'exit'

the music should continue.



[ Reply to This | # ]
Play Audio in Background?
Authored by: mario_grgic on Oct 06, '08 03:36:49PM

This means that your library is for the wrong architecture. You can find the file on another mac that has the right one and copy it over (I had to do that for one of my iMacs, which had the OS pre-installed by Apple. The Mac Pro, where I installed the OS form the DVDs had the right version).

Alternatively, you could find the file on the OS X installer DVD, but you will need a package manager/explorer for that.



[ Reply to This | # ]
Play Audio in Background?
Authored by: regulus on Oct 03, '08 10:10:47AM

Here's an applescript way...

set soundPath to choose file
do shell script "afplay " & quoted form of POSIX path of soundPath & " > /dev/null 2>&1 &"



[ Reply to This | # ]
Play Audio in Background?
Authored by: MacTipper on Oct 03, '08 10:31:07AM

Awesome! Thanks regulus!



[ Reply to This | # ]
Play Audio in Background?
Authored by: omaichotnho on Apr 25, '12 02:05:59AM

i'll try edit file hosts by type: sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
have a error: [IMG]http://banchothue.com/open-file-host-error.png [/IMG]
please help me!
thank off all!



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: Yuckandmuck on Oct 03, '08 10:17:58AM

If only this would work with a stream URL... Anyone know of a command line alternative for that?



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: funandblindness on Oct 03, '08 10:59:02PM
Mplayer svn checkout http://www.mplayerhq.hu/

svn://svn.mplayerhq.hu/mplayer/trunk mplayer

That baby does it all from the command line

[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: yDNA on Oct 03, '08 11:17:05AM

Does not work with protected audio files from iTunes, but QuickTime Player is able to play them.



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: darelon on Oct 05, '08 09:00:58AM
You might want to try another (third-party) command-line utility called play, which can play every audio file supported by QuickTime, incl. protected AAC files (as long as you machine is authorised).

[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: luigi193 on Oct 03, '08 02:25:11PM
"Is there a way to play the audio without having to keep the terminal window open?"

You can by putting an "&" at the end of the file. FOR EXAMPLE:
afplay /Users/Moo/Music/4 Tom Sawyer.mp3 &

You will then see this: [1] (random number) 63955
The song will then play in the background. You can stop by typing the word kill and the number it returns FOR EXAMPLE:
kill 63955

or you can more simply type in
killall afplay

to stop everything without having to worry about the number!
Hope that helps!
~Moo

[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: rpaege on Oct 03, '08 03:17:17PM

You can also chain several songs together by repeating the afplay command after a semicolon:

afplay music.mp3 &; afplay sound.mp3 &

etc..

I'm not a terminal geek so i had to fart around with this a bit to get it, but if I simply copy a file in the Finder then paste it after the afplay command (or simply drag it into the terminal) the path formatting will stay intact. So, that's probably the easiest thing to do for most people if they are getting file not found errors.



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: rahulbenegal on May 14, '10 10:36:30PM

Sorry, putting an ampersand & after each track means that all tracks will play in parallel. You should put one ampersand at the end.

afplay a.mp3 b.mp3 c.mp3 &



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: ataraxia on Oct 03, '08 05:14:14PM

Even more interesting to me is "afconvert", a front end to the various codecs supported by QuickTime.



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: funandblindness on Oct 04, '08 12:49:47AM

That is awesome. Got some converting to do.



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: el heffe on Oct 05, '08 07:24:56AM

This is awesome! I am deployed to Iraq and when I want to mess with my wife & son I ssh home and make the computer 'say' stuff. Now I can play them music in the AM!! YES!!! Thank you Mac OS X Hints, once again you are the *censored* of the walk! OK, I cannot say rooster's other names, so its censored, there by ruining my SNL reference.



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: tjfarrell on Oct 06, '08 02:16:02PM

There is also the "afinfo" command - gives you all the details on a sound file.


---
--
T. Farrell



[ Reply to This | # ]
10.5: Use a built-in command line audio player
Authored by: rahulbenegal on May 14, '10 10:47:06PM

Can I pause and resume an audio file using afplay ?



[ Reply to This | # ]