Contoh :
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.*;
import java.io.*;
public class AudioPlayerMidlet extends MIDlet implements CommandListener{
private Player player;
private VolumeControl vc;
private List lst;
private TextBox tb;
private Command back,exit,play,replay;
private Alert a1;
private String[] daftar = {
“drum”, “camera”, “boomerang”, “Instrumentalia midi” };
private String [] lagu={“/DRUMROLL.WAV”,”/CAMERA.WAV”,”/WHOOSH.WAV”,
/Dou.mid”};
public AudioPlayerMidlet(){
lst = new List(“DAFTAR LAGU”, List.IMPLICIT);
//masukin daftar string ke list
for(int i=0; i<daftar.length; i++){
lst.append(daftar[i], null);}
play = new Command(“Play”, Command.OK,1);
exit = new Command(“Exit”, Command.EXIT,1);
lst.addCommand(exit);
lst.addCommand(play);
lst.setCommandListener(this);
tb=new TextBox(“AUDIO DIMAINKAN “,”",50,1);
back = new Command(“Back”, Command.BACK,2);
replay = new Command(“Replay”, Command.OK,2);
tb.addCommand(back);
tb.addCommand(replay);
tb.setCommandListener(this);
Alert al = new Alert(“DAFTAR LAGU”);
al.setString(“Mohon tunggu sebentar…”);
Display.getDisplay(this).setCurrent(al,lst);
}
public void MainkanAudio(int pilih){
try{
InputStream is = getClass().getResourceAsStream(lagu[pilih]);
String tipe=”";
if (pilih==3)tipe=”audio/midi”;
else tipe=”audio/x-wav”;
player = Manager.createPlayer(is,tipe);
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl(“VolumeControl”);
if(vc != null) { vc.setLevel(10); }
player.start();
}
catch(Exception e){}
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(c == exit){
destroyApp(false);
notifyDestroyed();
}
else if(c == play){
int p=lst.getSelectedIndex();
tb.setString(“Audio Terpilih pilih : “+ daftar[p]);
Display.getDisplay(this).setCurrent( tb);
MainkanAudio(p);
}
if(c == back){
try {
player.stop();
}
catch (MediaException ex) {
ex.printStackTrace();
}
Display.getDisplay(this).setCurrent( lst);
startApp();
}
else if(c == replay){
int p=lst.getSelectedIndex();
try {
player.stop();
}
catch (MediaException ex) {
ex.printStackTrace();
}
MainkanAudio(p);
}
}
}
Tidak ada komentar:
Posting Komentar