J2ME播放视频Netbeans IDE 6.9,WTK 2.5.2,JDK 6u20

时间:2015-04-27 06:11:22

标签: java netbeans java-me java-wireless-toolkit

我正在尝试在J2Me中播放视频。 我正在遵循http://www.java2s.com/Tutorial/Java/0430__J2ME/DisplayVideo.htm

中的教程

这是我的代码:

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Canvas;
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.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) {
  }
}

但它给了我错误。

java.lang.OutOfMemoryError
        at javax.microedition.lcdui.List.callKeyPressed(List.java:819)
        at javax.microedition.lcdui.Display$DisplayAccessor.keyEvent(Display.java:2209)
        at javax.microedition.lcdui.Display$DisplayManagerImpl.keyEvent(Display.java:2952)
        at com.sun.midp.lcdui.DefaultEventHandler.keyEvent(DefaultEventHandler.java:249)
        at com.sun.midp.lcdui.AutomatedEventHandler.keyEvent(AutomatedEventHandler.java:620)
        at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(DefaultEventHandler.java:699)
        at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:608)

那么,我该怎么做才能解决这个问题呢? 它无法在模拟器上播放。 我的软件Netbeans IDE 6.9,JDK 6u20,WTK 2.5.2。

0 个答案:

没有答案