Java Tutorial/J2ME/ToneControl

Материал из Java эксперт
Перейти к: навигация, поиск

Use ToneControl

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.ToneControl;
import javax.microedition.midlet.MIDlet;
public class ToneMIDlet extends MIDlet implements CommandListener {
  private Display mDisplay;
  private List mMainScreen;
  public void startApp() {
    mDisplay = Display.getDisplay(this);
    if (mMainScreen == null) {
      mMainScreen = new List("AudioMIDlet", List.IMPLICIT);
      mMainScreen.addCommand(new Command("Exit", Command.EXIT, 0));
      mMainScreen.addCommand(new Command("Play", Command.SCREEN, 0));
      mMainScreen.setCommandListener(this);
    }
    mDisplay.setCurrent(mMainScreen);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT)
      notifyDestroyed();
    else
      run();
  }
  public void run() {
    byte[] sequence = null;
      sequence = new byte[] { ToneControl.VERSION, 1, ToneControl.TEMPO, 22,
          ToneControl.BLOCK_START, 0, 60, 8, 62, 4, 64,
          ToneControl.SET_VOLUME, 50, ToneControl.PLAY_BLOCK, 1, ToneControl.SET_VOLUME, 100,
          ToneControl.PLAY_BLOCK, 0, };
    try {
      Player player = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
      player.realize();
      ToneControl tc = (ToneControl) player.getControl("ToneControl");
      tc.setSequence(sequence);
      player.start();
    } catch (Exception e) {
      Alert a = new Alert("Exception", e.toString(), null, null);
      a.setTimeout(Alert.FOREVER);
      mDisplay.setCurrent(a, mMainScreen);
    }
  }
}