The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail

Trail: Essential leJOS classes
Lesson: Controlling the Hardware

Controlling the Hardware: Sound

The RCX contains a little speaker which is able to generate many simple sounds.
leJOS offers access to this audio capabilities via the josx.rcx.platform.Sound class.
This class is static; hence a typical call would be


            Sound.beep();
        
There are several methods available for producing sounds:

Note that the above calls (save systemSound()) return immediately; hence the program flow continues while the RCX is playing the sound(s) in question.

The variety of sounds proved to be pretty useful for means of error seeking: just play a special sound or sound sequence at a point in your code when you want to get sure that this part is actually executed.

An Example that uses the Sound class


            /**
            * plays some sounds
            */
            import josx.platform.rcx.*;
            class SoundExample {
                public static void main(String[] args) 
                    throws InterruptedException {
                    // play tone
                    Sound.playTone(4000,100);
                    // wait a bit
                    Thread.sleep(1500);
                    // play system sound (long low beep)
                    Sound.systemSound(false,4);
                    // wait a bit
                    Thread.sleep(2000);
                } // main()
            } // class SoundExample
        

The Sound API

may be found here.
The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail