Cached Audio Player : Audio Player « J2ME « Java Tutorial






import java.util.Enumeration;
import java.util.Hashtable;

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 CachedAudioPlayerCanvas implements ItemStateListener {
  private CachingAudioPlayer 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;

  private Hashtable players = new Hashtable();

  public CachedAudioPlayerCanvas(CachingAudioPlayer 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 = (Player) players.get(locator);
      if (player == null) {
        player = Manager.createPlayer(getClass().getResourceAsStream(locator), "audio/x-wav");
        player.prefetch();
        players.put(locator, player);
      }
      volume = (VolumeControl) player.getControl("VolumeControl");
      volume.setLevel(50);
      gauge.setValue(volume.getLevel());
      gauge.setLabel("Volume: " + volume.getLevel());
      player.setLoopCount(2);
      player.start();
      form.setTitle("Playing " + locator);
    } 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) {
      try {
        player.setMediaTime(0);
      } catch (Exception e) {
      }

      player.deallocate();
      player = null;
    }
  }

  public void closeAll() {
    for (Enumeration e = players.elements(); e.hasMoreElements();) {
      Player p = (Player) e.nextElement();
      p.close();
    }
  }
}

public class J2MECachingAudioPlayer extends MIDlet implements CommandListener {

  private String[] audioDisplayList = { "B", "A", "L" };

  private String[] audioList = { "/b.wav", "/a.wav", "/l.wav" };

  protected Display display;

  private CachedAudioPlayerCanvas 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 J2MECachingAudioPlayer() {
    list.addCommand(exitCommand);
    list.setCommandListener(this);
    canvas = new CachedAudioPlayerCanvas(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.closeAll();
  }

  public void commandAction(Command command, Displayable disp) {
    if (command == exitCommand) {
      canvas.closeAll(); 
      notifyDestroyed(); 
      return;
    } else if (command == backCommand) {
      canvas.cleanUp();
      display.setCurrent(list);
      return;
    }
  }
}








31.54.Audio Player
31.54.1.Audio Player Demo
31.54.2.Cached Audio Player
31.54.3.Wav file player
31.54.4.Player Event
31.54.5.implements PlayerListener
31.54.6.Network Player Manager
31.54.7.Load WAV file from a web site