Java Reference
In-Depth Information
■
Tip
As with the
getSnapshot
method, different devices may provide different encoding schemes, and
media recorded on one device may not be playable on a different vendor's device. When designing your
application, think carefully about the needs of different devices for specific media types, and keep in mind
the possibility that your application will need a specific component (usually hosted on a server somewhere)
to transcode media from one format to another.
Playing Individual Tones
One of my first experiences with writing a multimedia application wasn't on a computer,
but rather on a programmable calculator. It had a
BEEP
instruction that let you play a
four-tone beep encoded in ROM, and a
TONE
instruction that let you play a tone with a
preassigned frequency. I thought this was the bee's knees at the time; being able to play a
single tone is still useful for some applications.
The easiest way to play a tone or two is with the
Manager
's
playTone
method, which
takes a note, duration in milliseconds, and volume, like this:
Manager.playTone( 60, 3000, 100 );
The note value is actually a Musical Instrument Digital Interface (MIDI) note selec-
tor. Middle C is arbitrarily given the value
60
, and you count up or down by half steps
from Middle C (e.g., A above Middle C would be
69
). As simple as it is,
playTone
can
throw a
MediaException
, just like any player might, because another application could
be using the sound hardware. It can also throw an
IllegalArgumentException
if the tone
you provide is out of range.
If you want to play tones in sequence, it's better if you use a
Player
instance with an
associated
ToneControl
instance, because then you can use the
Player
to control the
sequencing of each tone. Follow these steps to play a sequence of tones:
1.
Create a
Player
instance using the locator
Manager.TONE_DEVICE_LOCATOR
(which
evaluates to
device://tone
).
2.
Realize the player.
3.
Get a
ToneControl
instance by invoking
player.getControl("ToneControl")
.
4.
Pass a sequence of tones encoded as an array of bytes (
byte[]
) to the
ToneControl
instance you obtained in the previous step using the
ToneControl
's
setSequence
method.
5.
Start the
Player
instance.