Moveable Video : Video « J2ME « Java Tutorial






import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDlet;

public class MoveableVideoMIDlet extends MIDlet implements CommandListener {
  private Player player = null;

  private VideoControl videoControl = (VideoControl) player
      .getControl("javax.microedition.media.control.VideoControl");

  private VolumeControl volControl = (VolumeControl) player
      .getControl("javax.microedition.media.control.VolumeControl");

  private MovableVideoCanvas canvas = new MovableVideoCanvas(videoControl);

  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private Command stopAudioCommand = null;

  private Display display = Display.getDisplay(this);

  private Alert alert = new Alert("Error");

  public MoveableVideoMIDlet() {
    alert.addCommand(exitCommand);
    alert.setCommandListener(this);

    try {
      player = Manager.createPlayer(getClass().getResourceAsStream(
          "/r.mp4"), "video/mp4");
      player.realize();

      stopAudioCommand = new Command("Stop Audio", Command.SCREEN, 1);

      canvas.addCommand(exitCommand);
      canvas.addCommand(stopAudioCommand);
      canvas.setCommandListener(this);

      videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);

      int halfCanvasWidth = canvas.getWidth() / 2;
      int halfCanvasHeight = canvas.getHeight() / 2;

      videoControl.setDisplayFullScreen(false);
      videoControl.setDisplaySize(halfCanvasWidth, halfCanvasHeight);
      videoControl.setDisplayLocation(halfCanvasWidth / 2,
          halfCanvasHeight / 2);
      videoControl.setVisible(true);
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public void startApp() {
    try {
      player.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
    display.setCurrent(canvas);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
    try {
      if (player != null)
        player.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == exitCommand) {
      destroyApp(true);
      notifyDestroyed();
    } else if (cmd == stopAudioCommand) {
      if (volControl != null)
        volControl.setMute(true);
    }
  }
}

class MovableVideoCanvas extends GameCanvas {
  private VideoControl videoControl = null;

    public MovableVideoCanvas(VideoControl vControl) {
    super(false);
    this.videoControl = vControl;
  }
  public void paint(Graphics g) {
    flushGraphics();
  }

  public void keyPressed(int keyCode) {
    int y = videoControl.getDisplayY();
      y -= 2;
    videoControl.setDisplayLocation(videoControl.getDisplayX(), y);
    repaint();
  }

}








31.51.Video
31.51.1.Display VideoDisplay Video
31.51.2.Moveable Video