background image
C H A P T E R 1 4
Using Sound
Synchronous and Asynchronous Sound
When a sound is played asynchronously, the playback can be intermixed with other
tasks because the system does not wait for the sound to finish before beginning
another task (such as updating the user interface, allowing user feedback; for
example with buttons, or playing a subsequent sound).
When playback must be allowed to complete, use the
, or
to guarantee
uninterrupted playback. Synchronous playback is generally preferred unless the
sound is so long as to be tedious or the application requires a high degree of
responsiveness to the user. The NewtonScript interpreter can do nothing else until
it completes synchronous playback.
Both approaches have benefits and drawbacks: synchronous playback can block
other NewtonScript code from running when it's inconvenient to do so; on the
other hand, asynchronous playback is never guaranteed to complete. Your use of
synchronous or asynchronous sound playback depends on your application's needs.
Differences Between Synchronous Asynchronous Playback
The following code example demonstrates the difference between asynchronous
playback and synchronous playback. To hear the demonstration of the two types
of sound playback, type following code example into the Inspector as it is shown
here, select all of these lines, and press Enter:
print ("Synchronous sound demo");
call func()
for i := 0 to 20 do
end with();
print ("Async sound demo");
call func()
for i := 0 to 20 do
end with();
The synchronous sound playback example plays the
twenty times; the sound plays its entire length each time. Twenty repetitions may
seem a bit laborious until you hear how quickly the same calls are made in
asynchronous mode.
Note that the asynchronous version can call the sound chip so fast that the sound
does not have enough time to finish playing; as a result, part of the playback is
© 2007-2017, o7 studio » при воспроизведении материала сайта ссылка обязательна