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 Resource
You can play a sound stored in a resource by calling theSndPlay
function, which requires a handle to an existing'snd '
resource. An'snd '
resource contains sound commands that play the desired sound. The'snd '
resource might also contain sound data. If it does (as in the case of a sampled sound), that data might be either compressed or noncompressed.SndPlay
decompresses the data, if necessary, to play the sound. Listing 1-1 illustrates how to play a sound resource.Listing 1-1 Playing a sound resource with
SndPlay
FUNCTION MyPlaySndResource (mySndID: Integer): OSErr; CONST kAsync = TRUE; {for asynchronous play} VAR mySndHandle: Handle; {handle to an 'snd ' resource} myErr: OSErr; BEGIN mySndHandle := GetResource('snd ', mySndID); myErr := ResError; {remember any error} IF mySndHandle <> NIL THEN {check for a NIL handle} BEGIN HLock(mySndHandle); {lock the sound data} myErr := SndPlay(NIL, mySndHandle, NOT kAsync); HUnlock(mySndHandle); {unlock the sound data} ReleaseResource(mySndHandle); END; MyPlaySndResource := myErr; {return the result} END;When you passSndPlay
aNIL
sound channel pointer in its first parameter, the Sound Manager automatically allocates a sound channel (in the application's heap) and then disposes of it when the sound has completed playing. Note, however, that when your application does passNIL
as the pointer to a sound channel, the third parameter toSndPlay
is ignored; the sound plays synchronously even if you specify that you want it to play asynchronously.
- IMPORTANT
- The handle you pass to
SndPlay
must be locked for as long as the sound is playing.