Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

General Structure

The API is based on three parts: Manager, Player, and Control; and two packages, javax.microedition.media and javax.microedition.media.control. The media package contains the Manager and possible Players, and the control package contains all the Controls.

The most important class of the API is Manager, which has the functionality to create different kinds of Players (for audio, video, etc.), to get supported protocols and content types and to play simple tone sequences. The Player plays multimedia content. The Control is an interface used to control Players. The Control controls volume, tone, pitch, and so on.

Figure 1 shows the relationships between Manager, Player, and Control.

Figure 1Figure 1 Manager creates Players, and Players have Controls.

Creating a player is similar to creating a (network) connection; it requires one or two method calls (of Manager), depending on the situation.

The following code creates a Player from an audio file located on the Internet, and then plays the audio:

try {
 Player audioPlayer =
  Manager.createPlayer("http://www.nullplace.com/music.wav");
 audioPlayer.start();
} catch (IOException ioe) {
} catch (MediaException me) { }

The next lines create a Player from an audio file stored in the .jar file of the application, and then play the audio:

try {
 InputStream is = getClass().getResourceAsStream("music.wav");
 Player audioPlayer = Manager.createPlayer(is, "audio/X-wav");
 audioPlayer.start();
} catch (IOException ioe) {
} catch (MediaException me) { }

Playing a Simple Tone

Playing a tone is a very simple process, using a handy method in the Manager class. The following MIDlet has two actions: one for exiting the application and one for playing a simple tone. Playing the tone uses the simpleTone() method with the Manager's playTone(int note, int duration, int volume) method.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class MediaMIDlet extends MIDlet implements CommandListener{
  private Player mp;
  private Display display;
  private List list;
  private Command exitCommand = new Command("Exit", Command.EXIT, 2);
  private Command playCommand = new Command("Play", Command.ITEM, 1);

  public MediaMIDlet() {
   display = Display.getDisplay(this);
   list = new List("Demo", List.IMPLICIT);
  }

  public void startApp() {
   list.addCommand(exitCommand);
   list.addCommand(playCommand);
   list.setCommandListener(this);

   display.setCurrent(list);
  }

  public void destroyApp(boolean unconditional) {
  }

  public void pauseApp() {
  }

  public void commandAction(Command c, Displayable s) {
   if (c == exitCommand) {
    destroyApp(false);
    notifyDestroyed();
   }
   if (c == playCommand) {
    simpleTone();
   }
  }

  private void simpleTone() {
   try {
     Manager.playTone(ToneControl.C4, 100, 80);
   } catch (Exception ex){}
  }
}

Setting the tone of the sound is a bit tricky, if the note is not middle C. As shown in the code above, there's a constant for middle C in the ToneControl class: ToneControl.C4. There's also a constant for silence: ToneControl.SILENCE. The following section describes how to use these building blocks to compose tone sequences.

Playing Tone Sequences

The following example introduces variables (bytes) for different notes. The most interesting part is building up the array mySequence:

  1. The array is filled with integers, which are either constant-value pairs or note-length pairs.

  2. The version and the tempo are set, and then blocks 0 and 1 (parts A and B).

  3. The parts are played.

After the tone sequence is ready, the rest of the code shows how to create a ToneControl and how to play the sequence with it.

  private void toneSequence() {
   byte tempo = 30;
   byte d = 8;

   byte C4 = ToneControl.C4;;
   byte D4 = (byte)(C4 + 2);
   byte E4 = (byte)(C4 + 4);
   byte F4 = (byte)(C4 + 5);
   byte G4 = (byte)(C4 + 7);
   byte rest = ToneControl.SILENCE;

   byte[] mySequence = {
    ToneControl.VERSION, 1,
    ToneControl.TEMPO, tempo,
    ToneControl.BLOCK_START, 0,  // starting A part
    C4,d, F4,d, F4,d, C4,d, F4,d, F4,d, C4,d, F4,d,
    ToneControl.BLOCK_END, 0,   // ending A part
    ToneControl.BLOCK_START, 1,  // starting B part
    C4,d, E4,d, E4,d, C4,d, E4,d, E4,d, C4,d, E4,d,
    ToneControl.BLOCK_END, 1,   // ending B part
    ToneControl.PLAY_BLOCK, 0,  // playing A part
    ToneControl.PLAY_BLOCK, 1,  // playing A part
    ToneControl.PLAY_BLOCK, 0,  // playing A part
   };

   try{
    Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
    p.realize();
    ToneControl c = (ToneControl)p.getControl("ToneControl");
    c.setSequence(mySequence);
    p.start();
   } catch (IOException ioe) {
   } catch (MediaException me) {}
  }
  • + Share This
  • 🔖 Save To Your Account