Thursday, November 6, 2014

Membuat MIDI Player sederhana PADA JAVA (J2ME)


import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;

public class MidiPlayer extends MIDlet
  implements CommandListener {
   
  private Display display;
  private List menu;
  private Form form;
  private Gauge volume;
 
  private Player player;
  private VolumeControl vc;

  private final Command cmdKeluar =
    new Command("Keluar", Command.EXIT, 1);
  private final Command cmdMainkan =
    new Command("Mainkan", Command.SCREEN, 1);
  private final Command cmdVolume =
    new Command("Set Volume", Command.SCREEN, 1);
  private final Command cmdOK =
    new Command("OK", Command.OK, 1);
  private final Command cmdBerhenti =
    new Command("Berhenti", Command.OK, 2);
 
  private final String[] FILE =
     {"File1.mid", "File2.mid", "File3.mid"};
 
  public MidiPlayer() {
    display = Display.getDisplay(this);
   
    menu = new List("MIDI Player", Choice.IMPLICIT, FILE, null);
          
    menu.addCommand(cmdKeluar);
    menu.addCommand(cmdMainkan);
    menu.addCommand(cmdVolume);
    menu.setCommandListener(this);   
  }
   
  public void startApp() {
    form = new Form("Volume");
    volume = new Gauge("Set Volume", true, 10, 5);
    display.setCurrent(menu);
  }
   
  public void pauseApp() {
  }
   
  public void destroyApp(boolean unconditional) {
  }
   
  public void commandAction(Command c, Displayable s) {
    if (c == cmdKeluar) {
      destroyApp(false);
      notifyDestroyed();
    } else if (c == cmdMainkan) {
      String namaFile = menu.getString(menu.getSelectedIndex());
      playMIDI(namaFile);
      menu.removeCommand(cmdMainkan);
      menu.removeCommand(cmdKeluar);
      menu.removeCommand(cmdVolume);
      menu.addCommand(cmdBerhenti);
    } else if (c == cmdBerhenti) {
      stopMIDI();
    } else if (c == cmdOK) {
      display.setCurrent(menu);
    } else {
      form.append(volume);
      form.addCommand(cmdOK);
      form.setCommandListener(this);
      display.setCurrent(form);
    }
  }
 
  public void playMIDI(String namaFile) {
    try {
      InputStream is = getClass().getResourceAsStream(namaFile);
      player = Manager.createPlayer(is, "audio/midi");
      player.addPlayerListener(new StopListener(this));
      player.realize();
      vc = (VolumeControl) player.getControl("VolumeControl");
      vc.setLevel(volume.getValue() * 10);
      player.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  public void stopMIDI() {
    try {
      player.stop();
      player.deallocate();
      player.close();
      player = null;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  // inner class
  class StopListener implements PlayerListener {
     
    private MidiPlayer midlet;
     
    public StopListener(MidiPlayer midlet) {
      this.midlet = midlet;
    }
     
    public void playerUpdate(Player player,
                             String event, Object data) {
      try {
        if (event.equals(STOPPED) ||
          event.equals(STOPPED_AT_TIME) ||
          event.equals(ERROR) ||
          event.equals(END_OF_MEDIA)) {
          player.deallocate();
          player.close();
          player = null;
          midlet.menu.removeCommand(midlet.cmdBerhenti);
          midlet.menu.addCommand(midlet.cmdMainkan);
          midlet.menu.addCommand(midlet.cmdVolume);
          midlet.menu.addCommand(midlet.cmdKeluar);
        } else if (event.equals(DEVICE_UNAVAILABLE)) {
          player.stop();
        } else if (event.equals(DEVICE_AVAILABLE)) {
          player.start();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }         
  }
 
}

No comments:

Post a Comment