Live Traffic

Our Story

Ceremony

Search

Just Married

Playing sound in j2me

by - May 03, 2010

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

public class CobaSuara extends MIDlet implements PlayerListener, CommandListener {
private int i = 1;
private Display display;
private Form form;
private List list;
private Image backGround;
private static Player player;
private VolumeControl vc;

private final Command cmdOpen = new Command("Open", Command.SCREEN, 1);
private final Command cmdExit = new Command("Exit", Command.EXIT, 1);
private final Command cmdClose = new Command("Close", Command.BACK, 1);

public CobaSuara() {
display = Display.getDisplay(this);
list = new List(null, Choice.IMPLICIT);

tampilAwal();
}

public void startApp() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
if(player != null){
player.close();
}
}

public void commandAction(Command c, Displayable s) {

if(c == cmdOpen) {

switch (list.getSelectedIndex()) {

case 0:

Suara();

try {

playMedia("/suara.wav");

} catch (Exception e) {

e.printStackTrace();

}

break;

}

} else if (c == cmdExit) {

exitMIDlet();

} else if (c == cmdClose) {

tampilAwal();

i = 1;

if(player != null) {

player.close();

}

}

}

public void tampilAwal() {

list.setTitle("Suara");

list.deleteAll();

list.append("Suara", null);

list.addCommand(cmdOpen);

list.addCommand(cmdExit);

list.setCommandListener(this);

display.setCurrent(list);

}

public Form Suara() {

form = new Form("Suara");

try {

backGround = Image.createImage("/Suara.PNG");

} catch (java.io.IOException e) {

}

form.append(new ImageItem(null, backGround,ImageItem.LAYOUT_CENTER, null));

form.addCommand(cmdClose);

form.setCommandListener(this);

display.setCurrent(form);

return form;

}

private void playMedia(String file) throws Exception {

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

player.addPlayerListener(this);

player.setLoopCount(-1);

player.prefetch();

player.realize();

vc = (VolumeControl)player.getControl("VolumeControl");

if (vc != null)

vc.setLevel(100);

player.start();

}

public void playerUpdate(Player player, String event, Object eventData) {

if(event.equals(PlayerListener.STARTED) &&

new Long(0L).equals((Long)eventData)) {

} else if(event.equals(PlayerListener.CLOSED)) {

}

}

public void exitMIDlet() {

destroyApp(false);

notifyDestroyed();

}

}

You May Also Like

0 comments