Java Tutorial/J2ME/Video

Материал из Java эксперт
Перейти к: навигация, поиск

Display Video

   <source lang="java">

import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.List; import javax.microedition.lcdui.StringItem; import javax.microedition.media.Manager; import javax.microedition.media.Player; import javax.microedition.media.control.GUIControl; import javax.microedition.media.control.VideoControl; import javax.microedition.midlet.MIDlet; public class DisplayVideoMIDlet extends MIDlet implements CommandListener {

 private List list = new List("Pick One", List.IMPLICIT);
 private Canvas canvas = new VideoCanvas();
 private Form form = new Form("Video Form", null);
 private StringItem descriptionItem = new StringItem("Desc: ", "Bad audio");
 Player player = null;
 private Command backCommand = new Command("Back", Command.ITEM, 1);
 private Command exitCommand = new Command("Exit", Command.EXIT, 1);
 private Alert alert = new Alert("Error");
 private Display display = null;
 private boolean error = false;
 public DisplayVideoMIDlet() {
   display = Display.getDisplay(this);
   canvas.addCommand(exitCommand);
   canvas.addCommand(backCommand);
   canvas.setCommandListener(this);
   form.append(descriptionItem);
   form.addCommand(exitCommand);
   form.addCommand(backCommand);
   form.setCommandListener(this);
   list.append("Play Video on Form", null);
   list.append("Play Video on Canvas", null);
   list.addCommand(exitCommand);
   list.setCommandListener(this);
 }
 public void startApp() {
   if (error)
     return;
   display.setCurrent(list); 
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
   try {
     if (player != null)
       player.close();
   } catch (Exception e) {
     error(e);
   }
 }
 public void commandAction(Command cmd, Displayable disp) {
   if (cmd == exitCommand) {
     destroyApp(true);
     notifyDestroyed();
   } else if (cmd == backCommand) { 
     try {
       if (player != null)
         player.close();
     } catch (Exception e) {
       error(e);
     }
     display.setCurrent(list);
     return;
   }
   try {
     loadPlayer();
     if (list.getSelectedIndex() == 0) { 
       GUIControl guiControl = (GUIControl) player .getControl("javax.microedition.media.control.GUIControl");
       if (guiControl == null)
         throw new Exception("No GUIControl!!");
       Item videoItem = (Item) guiControl.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE, null);
       form.insert(0, videoItem);
       display.setCurrent(form);
       player.start();
     } else {
       VideoControl videoControl = (VideoControl) player.getControl("javax.microedition.media.control.VideoControl");
       if (videoControl == null)
         throw new Exception("No VideoControl!!");
       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
       videoControl.setDisplayFullScreen(true);
       videoControl.setVisible(true);
       display.setCurrent(canvas);
       player.start();
     }
   } catch (Exception e) {
     error(e);
   }
 }
 private void loadPlayer() throws Exception {
   player = Manager.createPlayer(getClass().getResourceAsStream("/r.mp4"), "video/mpeg4");
   player.realize();
 }
 private void error(Exception e) {
   alert.setString(e.getMessage());
   alert.setTimeout(Alert.FOREVER);
   display.setCurrent(alert);
   e.printStackTrace();
   error = true;
 }

} class VideoCanvas extends Canvas {

 public void paint(Graphics g) {
 }

}</source>





Moveable Video

   <source lang="java">

import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; 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();
 }

}</source>