Mixing Audio : MIDI « J2ME « Java Tutorial






import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.MIDIControl;
import javax.microedition.midlet.MIDlet;

public class MixingAudioMIDlet extends MIDlet implements CommandListener {
  private Player backgroundPlayer = null;

  private Player firePlayer = null;

  private Player jumpPlayer = null;

  private MIDIControl mControl = null;

  private Display display = null;

  private Alert alert = new Alert("Message");

  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private Command fireCommand = new Command("Fire!", Command.SCREEN, 1);

  private Command jumpCommand = new Command("Jump!", Command.SCREEN, 1);

  public MixingAudioMIDlet() {
    display = Display.getDisplay(this);
    alert.setString("Playing background score");
    alert.setTimeout(Alert.FOREVER);
    alert.addCommand(exitCommand);
    alert.addCommand(fireCommand);
    alert.addCommand(jumpCommand);

    alert.setCommandListener(this);

    initPlayers();
  }

  private void initPlayers() {
    try {
      backgroundPlayer = Manager.createPlayer(getClass().getResourceAsStream("/a.mid"),
          "audio/midi");

      backgroundPlayer.prefetch();
      backgroundPlayer.setLoopCount(-1);

      firePlayer = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR);
      firePlayer.prefetch();

      mControl = (MIDIControl) firePlayer
          .getControl("javax.microedition.media.control.MIDIControl");

      mControl.setProgram(11, -1, 127);

      jumpPlayer = Manager.createPlayer(getClass().getResourceAsStream(
          "/p.wav"), "audio/x-wav");

      jumpPlayer.prefetch();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void startApp() {
    try {
      if (backgroundPlayer != null) {
        backgroundPlayer.start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    display.setCurrent(alert);
  }

  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == exitCommand) {
      destroyApp(true);
      notifyDestroyed();
      return;
    }

    try {
      if (cmd == fireCommand) {
        mControl.shortMidiEvent(MIDIControl.NOTE_ON | 11, 60, 100);
      }
      if (cmd == jumpCommand) {
        jumpPlayer.start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public void pauseApp() {
    try {
      if (backgroundPlayer != null)
        backgroundPlayer.stop();
      if (firePlayer != null)
        firePlayer.stop();
      if (jumpPlayer != null)
        jumpPlayer.stop();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void destroyApp(boolean unconditional) {
    try {
      if (backgroundPlayer != null) {
        backgroundPlayer.close();
        backgroundPlayer = null;
      }
      if (firePlayer != null) {
        firePlayer.close();
        firePlayer = null;
      }
      if (jumpPlayer != null) {
        jumpPlayer.close();
        jumpPlayer = null;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}








31.57.MIDI
31.57.1.Play MIDI
31.57.2.Controllable MIDI
31.57.3.MIDI Capabilities
31.57.4.MIDI EventsMIDI Events
31.57.5.Mixing Audio