Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!apple!han From: han@Apple.COM (Byron Han, wyl E. coyote ) Newsgroups: comp.sys.mac.programmer Subject: Re: SoundPlay in Async mode Message-ID: <28665@apple.Apple.COM> Date: 9 Apr 89 17:42:45 GMT References: <1354@ndmath.UUCP> Organization: Apple R&D - Networking & Communications Lines: 84 In article <1354@ndmath.UUCP> milo@ndmath.UUCP (Greg Corson) writes: > >Would anyone happen to have a code fragment that illustrates how to setup >the required callback routines so you can have SoundPlay play a sound in the >background (async) mode? > >1. open the channel and specify a callback >2. play a snd resource >3. have the callback set a global flag >4. have my main event loop close the channel when the flag is set. This is the code I am using. There is NO guarantee or warranty with this code. Use at your own risk! This is NOT Apple DTS sanctioned code. 1. the main application needs to keep a global variable is a SndChannelPtr. Initialize it to NIL. 2. Pass in the handle to the snd resource that you want to play. This should bootstrap to you greater understanding of the world of the Sound Manager. PROCEDURE PlayIt(VAR chan: SndChannelPtr; theSound: Handle); VAR err: OSErr; myWish: SndCommand; BEGIN IF chan <> NIL THEN {cancel sound in progress} BEGIN err := SndDisposeChannel(chan, TRUE); {TRUE means now} IF err <> noErr THEN BEGIN DebugStr('Cannot dispose of old channel'); Exit(PlayIt); END ELSE chan := NIL; END; err := SndNewChannel(chan, 0, 0, NIL); IF err <> noErr THEN BEGIN DebugStr('Cannot allocate new channel'); Exit(PlayIt); END; IF theSound <> NIL THEN {we have a handle} IF theSound^ <> NIL THEN {and is not a nil handle } BEGIN err:=SndPlay(chan, theSound, TRUE); IF err <> noErr THEN BEGIN DebugStr('Cannot play sound'); Exit(PlayIt); END; WITH myWish DO { set up a sound mgr command block } BEGIN cmd := freeCmd; {to free self when done } param1 := 0; param2 := 0; END; {with} err := SndDoCommand(chan, myWish, FALSE); IF err <> noErr THEN BEGIN DebugStr('cannot add command'); Exit(PlayIt); END; END; END; +-----------------------------------------------------------------------------+ | Disclaimer: Apple has no connection with my postings. | +-----------------------------------------------------------------------------+ Byron Han, Communications Architect Cereal, anyone? :-) A1! Apple Computer, Inc. ------------------------------------- 20525 Mariani Ave, MS27Y Internet: han@apple.COM Cupertino, CA 95014 UUCP:{sun,voder,nsc,decwrl}!apple!han -------------------------------------- GENIE: BYRONHAN ATTnet: 408-974-6450 Applelink: HAN1 CompuServe: 72167,1664 ------------------------------------------------------------------------------