- Understanding the Java WTK
- The RTTTLEx Application
- Creating a List of Ring Tones
- Understanding the MIDP List Class
- Working with the Display Object and the Displayable Interface
- Configuring Commands
- Registering CommandListeners
- The Methods of a MIDlet
- Selecting a Tune
- The MIDP Canvas Object
- Playing a Tune
- Conclusion
The MIDP Canvas Object
I encapsulate RTTTL tune-playing functionality in the object RTTTLExCanvas and derive RTTTLExCanvas from the MIDP Canvas object. The code in Listing 8 shows an additional constructor that I designed for the RTTTLExCanvas object.
Listing 8 The RTTTLExCanvas Constructor
public RTTTLExCanvas(Display parentDisplay, String[] captions, String[] urls) { super(); this.captions = captions; this.urls = urls; this.idx = 0; this.parentDisplay = parentDisplay; initialize(); }
The constructor takes three parameters:
Display object that denotes the current displayable object
String array of captions
String array of URLs associated with the captions
The values of these parameters are set to private member variables within the class. We'll use the captions and urls arrays to determine which tune to play, and use the parentdisplay value to make the RTTTLExCanvas aware of the display of the mobile device. The display parameter is necessary in the event that something else must be displayed: an error message, or device permission prompts (such as consent to access data on the Internet).
Notice the call to the initialize() method. This method contains code that registers a BACK command to the display and sets the RTTTLExCanvas object to the CommandListener. If we didn't set the RTTTLExCanvas object as the listener, keyboard commands would not be recognized by the class.
Tunes are selected by moving up and down through the list onscreen and pressing the Play button once a tune is selected. When the user selects a tune from the list, the following code is invoked in the RTTTLExCanvas object's run() method to play the tune:
player = new RTTTLPlayerLite(); player.playRTTTL(urls[idx],this.parentDisplay);
NOTE
Tunes must be played in a separate thread to avoid message blocking. In Java, the run() method is required for running threads. For more information on creating and running threads, click here.
Obviously, I can't cover all the nuances of thread execution in the WTK in this short article. This topic is covered extensively in the WTK documentation.
Notice that in the code snippet above, a variable, player, is created as an instance of the RTTTLPlayerLite class. I'll discuss RTTTLPlayerLite in the next section. But for now the most important thing to know is that RTTTLPlayerLite has a method called playRTTTL() that plays a ring tone. This method takes two arguments: the URL where the ring tone data lives, and the current display. The tune's URL is determined as an element of the private array urls. The specific array element is determined by the value of the private member variable idx. idx is set indirectly from within RTTTLEx in the following code snippet from Listing 7:
int i = theList.getSelectedIndex(); //Let the tune player, RTTTLExCanvas, know which //tune has been selected player.setIndex(i);
The index, i, determined in RTTTLEx by calling the List object's getSelectedIndex() method and passed to RTTTLExCanvas via its setIndex() method, is assigned to the private data member idx. idx is then used as the offset in the urls array to determine which URL to call on the Internet (see Figure 7).
Figure 7 A tune is selected in RTTTLEx and played in the class RTTTLExCanvas.