Mega Code Archive

 
Categories / Java Tutorial / J2ME
 

Audio Player Demo

import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Gauge; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.ItemStateListener; import javax.microedition.lcdui.List; import javax.microedition.media.Manager; import javax.microedition.media.Player; import javax.microedition.media.control.VolumeControl; import javax.microedition.midlet.MIDlet; class AudioPlayerCanvas implements ItemStateListener {   private AudioPlayer parent;   private Form form = new Form("");   private Gauge gauge = new Gauge("Volume: 50", true, 100, 50);   private VolumeControl volume;   private Player player;   private boolean paused = false;   public AudioPlayerCanvas(AudioPlayer parent) {     this.parent = parent;     form.append(gauge);     form.addCommand(parent.exitCommand);     form.addCommand(parent.backCommand);     form.setCommandListener(parent);     form.setItemStateListener(this);   }   public void playMedia(String locator) {     try {       player = Manager.createPlayer(getClass().getResourceAsStream(locator), "audio/x-wav");       player.realize();       volume = (VolumeControl) player.getControl("VolumeControl");       volume.setLevel(50);       gauge.setValue(volume.getLevel());       gauge.setLabel("Volume: " + volume.getLevel());       player.setLoopCount(2);       player.start();     } catch (Exception e) {       e.printStackTrace();     }   }   public void pauseMedia() {     if (player != null) {       try {         player.stop();         paused = true;       } catch (Exception e) {       }     }   }   public void restartMedia() {     if (player != null) {       try {         player.start();         paused = false;       } catch (Exception e) {       }     }   }   public boolean isPlayerPaused() {     return paused;   }   public Form getForm() {     return this.form;   }   public void itemStateChanged(Item item) {     volume.setLevel(gauge.getValue());     gauge.setLabel("Volume: " + volume.getLevel());   }   public void cleanUp() {     if (player != null) {       player.close();       player = null;     }   } } public class J2MEAudioPlayer extends MIDlet implements CommandListener {   private String[] audioDisplayList = { "y.wav", "e.wav", "r.wav" };   private String[] audioList = { "/y.wav", "/e.wav", "/r.wav" };   protected Display display;   private AudioPlayerCanvas canvas;   private List list = new List("Pick an Audio file", List.IMPLICIT, audioDisplayList, null);   protected Command exitCommand = new Command("Exit", Command.EXIT, 1);   protected Command backCommand= new Command("Back", Command.BACK, 1);   public J2MEAudioPlayer() {     list.addCommand(exitCommand);     list.setCommandListener(this);     canvas = new AudioPlayerCanvas(this);     display = Display.getDisplay(this);   }   public void startApp() {     if (canvas.isPlayerPaused()) {       canvas.restartMedia();       display.setCurrent(canvas.getForm());     } else {       display.setCurrent(list);     }   }   public void pauseApp() {     canvas.pauseMedia();   }   public void destroyApp(boolean unconditional) {     canvas.cleanUp();   }   public void commandAction(Command command, Displayable disp) {     if (command == exitCommand) {       canvas.cleanUp();        notifyDestroyed();       return;     } else if (command == backCommand) {        canvas.cleanUp();       display.setCurrent(list);       return;     }     if (disp == list) {       canvas.playMedia(audioList[list.getSelectedIndex()]);       display.setCurrent(canvas.getForm());     }   } }