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, andSndStopFilePlayfunctions. You useSndStartFilePlayto initiate the playing of a sound file. If you allocate your own sound channel and specify that play be asynchronous, you can then use theSndPauseFilePlayandSndStopFilePlayfunctions 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
SndStartFilePlaythe 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 specifyNILas the sound channel, thenSndStartFilePlayallocates 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 passNILas the first and fifth parameters toSndStartFilePlay, as done in Listing 1-2. The firstNILspecifies that you wantSndStartFilePlayto allocate a sound channel itself. TheNILpassed as the fifth parameter specifies thatSndStartFilePlayshould automatically allocate buffers to play the sound. TheSndStartFilePlayfunction 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
SndStartFilePlayhere is set to 0. That parameter is used only when playing sound resources from disk.The seventh parameter to
SndStartFilePlayallows 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 toSndStartFilePlayto request synchronous playback.SndStartFilePlaywould return abadChannelresult code if you request asynchronous playback becauseMyPlaySoundFiledoes not allocate a sound channel.