Previous Book Contents Book Index Next

Inside Macintosh: Sound /
Chapter 1 - Introduction to Sound on the Macintosh / Using Sound on Macintosh Computers


Legacy Documentclose button

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 the SndPlay 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 pass SndPlay a NIL 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 pass NIL as the pointer to a sound channel, the third parameter to SndPlay 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.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996