Mark Chauvin wrote:
> Is there a way to play a sound in a Palm program
> without halting the processor while it plays?  I tried
> playing the alarm sound, but that gives me a
> 1.5-second gap in the data that my program is
> collecting.

There is a way to do it on devices with the Sound Stream
Feature set.  (Which in practice is, I think, all OS 5
devices.)

The obvious way would SEEM to be to use SndPlayResource() with the
sndFlagAsync flag.  However, the documentation has this to say:

        You should always specify sndFlagSync unless you can
        guarantee two things:

           1. Your resource memory remains locked for the
              duration of the sound
           2. Your application does not exit for the duration
              of the sound

There's only one problem with fulfilling these two conditions:
there is no supported way to know when the sound has finished
playing!  The best you can do is read the clock when you start
playing the sound and make a conservatively large guess about
how long it will take for the sound to play, then free the
memory after that time.  However, the sound is playing in a
thread behind the scenes, so this working depends on the thread
making progress.

Luckily, there is another way to do it that makes it possible.
You can put a raw PCM sample in a resource (or anywhere you want,
really, dynamic heap, whatever), then call SndStreamCreate()
or SndStreamCreateExtended() and give a callback that will be
used to fill the buffer.  When the callback finishes copying
from the source PCM data and the sound has finished playing,
the callback can call SndStreamStop() and then SndStreamDelete(),
and then clean up the source PCM data by unlocking the resource,
freeing dynamic memory, or whatever.

The one caveat with this is that, as far as I know, there is
no provision for automatically stopping the playback of a
sound when your app exits.  If you have a locked resource
record, it seems bad to keep it locked and keep accessing
it after the app is supposed to be closed.  :-)  You might
be able to get away with continuing to play the sound if had
copied the sound into the dynamic heap or a feature pointer
or something like that.  However, I would lean towards setting
some kind of flag when the sound starts and clearing it when
the sound finishes.  You could add an entry to a linked list
every time a sound starts, and put the flag in the node, so
that there is a separate flag for every sound playing.  Then
at app exit, scan the linked list to see if any sounds are
still playing; if so, go to sleep and poll again a few ticks
later.  Seems overly complicated, but I can't think of a
simpler way to properly handle app exit if a stream is still
playing...

  - Logan

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please 
see http://www.palmos.com/dev/support/forums/

Reply via email to