Important: Inside Macintosh: Sound is deprecated as of Mac OS X v10.5. For new audio development in Mac OS X, use Core Audio. See the Audio page in the ADC Reference Library.
Playing a Sound File
You can initiate and control a playback of sampled sounds stored in a file using theSndStartFilePlay
,SndPauseFilePlay
, andSndStopFilePlay
functions. You useSndStartFilePlay
to initiate the playing of a sound file. If you allocate your own sound channel and specify that play be asynchronous, you can then use theSndPauseFilePlay
andSndStopFilePlay
functions to pause, resume, and stop sound files that are playing. The chapter "Sound Manager" in this book describes these two functions in detail.To play a sampled sound that is contained in a file, you pass
SndStartFilePlay
the file reference number of the file to play. The sample should be stored in either AIFF or AIFF-C format. If the sample is compressed, it is automatically expanded during playback. If you specifyNIL
as the sound channel, thenSndStartFilePlay
allocates memory for a channel internally. Listing 1-2 defines a function that plays a file specified by its file reference number.Listing 1-2 Playing a sound file with
SndStartFilePlay
FUNCTION MyPlaySoundFile (myFileRefNum: Integer): OSErr; CONST kAsync = TRUE; {for asynchronous play} kBufferSize = 20480; {20K play buffer} VAR myErr: OSErr; BEGIN myErr := SndStartFilePlay(NIL, myFileRefNum, 0, kBufferSize, NIL, NIL, NIL, NOT kAsync); MyPlaySoundFile := myErr; END;To allow the Sound Manager to handle all memory allocation automatically, you should passNIL
as the first and fifth parameters toSndStartFilePlay
, as done in Listing 1-2. The firstNIL
specifies that you wantSndStartFilePlay
to allocate a sound channel itself. TheNIL
passed as the fifth parameter specifies thatSndStartFilePlay
should automatically allocate buffers to play the sound. TheSndStartFilePlay
function then allocates two buffers, each half the size specified in the fourth parameter; if the fourth parameter is 0, the Sound Manager chooses a default size for the buffers.The third parameter passed to
SndStartFilePlay
here is set to 0. That parameter is used only when playing sound resources from disk.The seventh parameter to
SndStartFilePlay
allows you to specify a routine to be executed when the sound finishes playing. This is useful only for asynchronous play. In Listing 1-2, the valueNOT kAsync
(that is,FALSE
) is passed as the eighth parameter toSndStartFilePlay
to request synchronous playback.SndStartFilePlay
would return abadChannel
result code if you request asynchronous playback becauseMyPlaySoundFile
does not allocate a sound channel.