Java Reference
In-Depth Information
byte rest = ToneControl.SILENCE;
byte block1 = 0;
byte block2 = 1;
byte[] song = new byte[] {
// As transcribed in JSR-135
ToneControl.VERSION, 1,
ToneControl.TEMPO, tempo,
ToneControl.SET_VOLUME, 50,
ToneControl.BLOCK_START, block1,// Start defining a block
E4,d, D4,d, C4,d, E4,d, // content of block 1
E4,d, E4,d, E4,d, rest,d,
ToneControl.BLOCK_END, block1, // end block definition
ToneControl.BLOCK_START, block2,
D4,d, D4,d, D4,d, rest,d, // content of block 2
E4,d, G4,d, G4,d, rest,d,
ToneControl.BLOCK_END, block2,
ToneControl.PLAY_BLOCK, block1, // play the first block
ToneControl.PLAY_BLOCK, block2, // play the next block
D4,d, D4,d, E4,d, D4,d, C4,d // play the last section
};
try {
player = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
player.realize();
player.prefetch();
control = (ToneControl)player.getControl("ToneControl");
control.setSequence(song);
player.start();
} catch (MediaException pe) {}
catch (IOException ioe) {}
catch (Exception e) {}
Specifying sequences of notes in your application like this is cumbersome and
unwieldy, so I suggest that if you take this approach, generate the array of ToneControl
sequences and store them as resources in your JAR file, which you can load using
getClass().getResourceAsStream() .
Finally, a tip regarding whether to use the playTone method or a ToneControl
instance: playTone does not block the currently executing thread. If you want to
sequence multiple tones using playTone , you should do so in a separate thread and
have the thread sleep between each invocation. Otherwise, one playTone may overwrite
the next; at best, you'll hear silence, but you may just get garbled noise, too. For this
reason, if you want to play multiple tones, I suggest you use a Player instance con-
trolled by a ToneControl instance instead.
 
Search WWH ::




Custom Search