Java Tutorial/J2ME/MIDI
Controllable MIDI
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.PitchControl;
import javax.microedition.media.control.TempoControl;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDlet;
public class ControllableMIDIMIDlet extends MIDlet implements ItemStateListener {
Player midiPlayer = null;
VolumeControl volControl = null;
PitchControl pitchControl = null;
TempoControl tempoControl = null;
Form form = new Form("MIDI Player", null);
Gauge volGauge = new Gauge("Volume: 50", true, 100, 50);
Gauge pitchGauge = new Gauge("Pitch: 0", true, 10, 5);
Gauge tempoGauge = new Gauge("Tempo: 120", true, 30, 12);
public ControllableMIDIMIDlet() {
try {
midiPlayer = Manager.createPlayer(getClass().getResourceAsStream("/a.mid"), "audio/midi");
midiPlayer.prefetch();
volControl = (VolumeControl) midiPlayer.getControl("javax.microedition.media.control.VolumeControl");
pitchControl = (PitchControl) midiPlayer.getControl("javax.microedition.media.control.PitchControl");
tempoControl = (TempoControl) midiPlayer.getControl("javax.microedition.media.control.TempoControl");
form.append(volGauge);
form.append(tempoGauge);
form.append(pitchGauge);
form.setItemStateListener(this);
Display.getDisplay(this).setCurrent(form);
} catch (Exception e) {
System.err.println(e);
}
}
public void itemStateChanged(Item item) {
if (!(item instanceof Gauge))
return;
Gauge gauge = (Gauge) item;
int val = gauge.getValue();
if (item == volGauge) {
volControl.setLevel(val);
volGauge.setLabel("Volume: " + val);
}
if (item == tempoGauge && tempoControl != null) {
tempoControl.setTempo((val) * 10 * 1000);
tempoGauge.setLabel("Tempo: " + (val * 10));
}
if (item == pitchGauge && pitchControl != null) {
pitchControl.setPitch((val - 5) * 12 * 1000);
pitchGauge.setLabel("Pitch: " + (val - 5));
}
}
public void startApp() {
try {
if (midiPlayer != null) {
midiPlayer.start();
}
} catch (Exception e) {
System.err.println(e);
}
}
public void pauseApp() {
try {
if (midiPlayer != null) {
midiPlayer.stop();
}
} catch (Exception e) {
System.err.println(e);
}
}
public void destroyApp(boolean unconditional) {
try {
if (midiPlayer != null) {
midiPlayer.close();
}
} catch (Exception e) {
System.err.println(e);
}
}
}
MIDI Capabilities
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.MIDIControl;
import javax.microedition.midlet.MIDlet;
public class MIDICapabilitiesMIDlet extends MIDlet {
public void startApp() {
try {
Player p = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR);
p.prefetch();
MIDIControl mControl = (MIDIControl) p
.getControl("javax.microedition.media.control.MIDIControl");
String msg = mControl.isBankQuerySupported() ? "MIDIControl is fully supported"
: "Minimum MIDIControl is provided";
Display.getDisplay(this).setCurrent(new Alert("Message", msg, null, AlertType.INFO));
} catch (Exception e) {
System.err.println(e);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
MIDI Events
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.MIDIControl;
import javax.microedition.midlet.MIDlet;
public class MIDIEventsMIDlet extends MIDlet {
Display display = null;
Alert alert = new Alert("Message");
public MIDIEventsMIDlet() {
display = Display.getDisplay(this);
alert.setString("Working...");
alert.setTimeout(Alert.FOREVER);
}
public void startApp() {
display.setCurrent(alert);
try {
Player p = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR);
p.prefetch();
MIDIControl mControl = (MIDIControl) p
.getControl("javax.microedition.media.control.MIDIControl");
if (mControl == null)
throw new Exception("MIDIControl not available");
mControl.shortMidiEvent(MIDIControl.NOTE_ON | 11, 60, 100);
Thread.sleep(100);
mControl.shortMidiEvent(192 | 11, 14, 0);
Thread.sleep(100);
mControl.shortMidiEvent(MIDIControl.CONTROL_CHANGE | 11, 7, 50);
Thread.sleep(100);
mControl.shortMidiEvent(MIDIControl.NOTE_ON | 11, 60, 0);
} catch (Exception e) {
alert.setString(e.getMessage());
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Mixing Audio
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.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();
}
}
}
Play MIDI
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;
public class PlayMIDIMIDlet extends MIDlet {
Player midiPlayer = null;
public PlayMIDIMIDlet() {
try {
midiPlayer = Manager.createPlayer(getClass().getResourceAsStream("/a.mid"), "audio/midi");
} catch (Exception e) {
System.err.println(e);
}
}
public void startApp() {
try {
if (midiPlayer != null) {
midiPlayer.start();
}
} catch (Exception e) {
System.err.println(e);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}