Anybody know of something that will play a single .wav file, then immediately exit?

hbquikcomjamesl

Registered
I have a "startup sound" script on my Mac, that plays any one of a rotating series of .wav files, exactly once, when I sign on to my Mac. When I got started on this, I quickly found out that if I allow iTunes to play a sound file, it immediately imports it into the playlist (NOT desired behavior), and then immediately runs through the playlist (a behavior I find profoundly undesirable, regardless of the circumstances). So I found that QuickTime would at least playback a sound file exactly once, although it wouldn't exit immediately after doing so.

I came up with the following, which has the problem that QuickTime doesn't reliably exit. I've also come up with something similar, to play a specific sound to alert me to, for example, dental appointments, so that I can shut down and leave in time, but it doesn't even reliably play the sound.

Code:
property cycle : 0
set cycle to cycle + 1
if cycle > 7 then set cycle to 1


if cycle = 1 then
	playback("/Users/jamesl/Library/Sounds/halcircuits.wav")
else if cycle = 2 then
	playback("/Users/jamesl/Library/Sounds/helpyou.wav")
else if cycle = 3 then
	playback("/Users/jamesl/Library/Sounds/homcomp.wav")
else if cycle = 4 then
	playback("/Users/jamesl/Library/Sounds/parental.wav")
else if cycle = 5 then
	playback("/Users/jamesl/Library/Sounds/goodmorn.wav")
else if cycle = 6 then
	playback("/Users/jamesl/Library/Sounds/wlcomjp.wav")
else if cycle = 7 then
	playback("/Users/jamesl/Library/Sounds/eglanded.wav")
end if

to playback(aSound)
	tell application "QuickTime Player"
		activate
		
		try
			set frontDoc to front document
			close front document
		on error err number errNum
			if errNum is -1719 then
				-- There is no open document
			else if errNum is -10000 then
				-- Front doc exists, but does not really...
			else
				log err
			end if
		end try
		
		open aSound
		play front document
		-- Hide QTP
		tell application "System Events"
			keystroke "h" using command down
		end tell
		delay 8
		quit
		
	end tell
end playback
The ideal solution would be something completely headless, that doesn't have to be told to exit after playing the sound file.

--
JHHL
 

DeltaMac

Tech
If you just want to play one of those sounds, then you could use the QuickLook function for that.
All you need to do is select the sound file, and press your space bar. The sound file will run once. Press space again, and the file preview window disappears. You can Command-select ALL your .wav files, then press space bar to provide a way of selecting each sound individually, or simply let all the .wav files play consecutively.

That's just a way to preview the files (listening to the sounds, in this case) and doesn't give you a lot of flexibility.

I suspect this won't actually suit your use, but it does allow you to play an individual sound file just for a test.
 

DeltaMac

Tech
Anybody know of something that will play a single .wav file, then immediately exit?
Not really what you asked about in your first post!
If QuickLook is "perfect", then recall that it is a user method for manually viewing various types of files that are supported by the system, or that have system plugins available. Being a user convenience means (at least in my opinion), that you have various limitations. One being, it's a manual process (select file, or a list of files - then provide a way for the preview window to open, which is usually pressing the space bar)
Maybe there's some ideas here: http://hints.macworld.com/article.php?story=20100712090451517

Are you just wanting to play some random sound files? Or, do you have some grander purpose in mind - like using specific sounds to respond to specific actions? You did already mention having a particular sound, maybe in response to a calendar event of some kind???
What I'm asking: What is your ultimate goal with the sounds? You may get better answers if you have something specific in mind.
 

hbquikcomjamesl

Registered
Unfortunately, whether done with a sound file from a terminal window or from a DO SHELL SCRIPT statement in an AppleScript, all qlmanage does is open up a window. It doesn't actually play the sound, let alone "play the sound exactly once, then terminate."

In the case of the startup script, the desired behavior is simply for one of the seven specified sounds to play once I'm signed on, and the desktop initialization has been completed. In the case of the script that runs to alert me of, say, a dental appointment, the idea is to play a specific sound file as an alert.
 

hbquikcomjamesl

Registered
Ye vish. You'd have thought I'd have found this back in November!

http://hints.macworld.com/article.php?story=20081002080543392

All I neded to do was a Google search on "macintosh play .wav command line," and it would have been the very first thing I found:

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.
The tip is flagged "10.5 only," but in fact, it apparently works on everything since then as well, up to and including Mountain Lion.
 

DeltaMac

Tech
Tip was flagged "10.5 only", but the date was prior to 10.6 and later versions.

Anyway, good catch. I remember reading about this several years ago, but haven't ever used it.
Glad you found the hint (and helped me at the same time. too! )
 
Top