CommandResources类,用于存放一些常量
package com.xiaoyu.record.common;
public class CommandResources {
public static final String IMG_TYPE = "jpeg";
public static final int IMG_WIDTH = 128;
public static final int IMG_HEIGHT = 128;
private static int TXT_ID = 0;
public static final String TXT_T_APP = "Record sound & video";
public static final String TXT_T_CAM = "拍照";
public static final String TXT_T_REC = "录音";
//主界面List
public static final String TXT_I_CAMERA = "我要拍照";
public static final String TXT_I_RECORD = "我要录音";
public static final String TXT_I_ABOUT = "关于";
public static final String TXT_I_EXIT = "退出";
public static final String TXT_C_OK = "确定";
public static final String TXT_C_CAM = "拍照";
public static final String TXT_C_REC = "录音";
public static final String TXT_C_EXIT = "退出";
public static final String TXT_C_PAUSE = "暂停";
public static final String TXT_C_RESUME = "继续";
public static final String TXT_C_BACK = "返回";
public static final String TXT_C_REC_FIN = "完成";
public static final byte EVENT_EXIT = 0;
public static final byte EVENT_BACK = 1;
public static final byte EVENT_CAM = 2;//拍照
public static final byte EVENT_REC = 3;//录音
public static final byte EVENT_REC_PAUSE = 4;//暂停录音
public static final byte EVENT_REC_RESUME = 5;//继续录音
public static final byte EVENT_REC_FIN = 6;//完成录音
public static final byte EVENT_GO_CAM = 7;//进入拍照界面
public static final byte EVENT_GO_REC = 8;//进入录音界面
}
以下是控制器类:
package com.xiaoyu.record.controller;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.media.control.VideoControl;
import com.xiaoyu.record.common.CommandResources;
import com.xiaoyu.record.midlet.RecordMIDLet;
import com.xiaoyu.record.view.CameraForm;
import com.xiaoyu.record.view.MainMenuList;
import com.xiaoyu.record.view.RecordForm;
public class GUIController {
private RecordMIDLet midlet;
private MainMenuList mainMenuList;
private CameraForm cameraForm;
private RecordForm recordForm;
private Player videoPlayer;
private Player recordPlayer;
private VideoControl videoControl;
private RecordControl recordControl;
private ByteArrayOutputStream baos = null;
public GUIController(RecordMIDLet recordMIDLet) {
this.midlet = recordMIDLet;
}
public void init() {
mainMenuList = new MainMenuList(CommandResources.TXT_T_APP, this);
cameraForm = new CameraForm(CommandResources.TXT_T_CAM, this);
recordForm = new RecordForm(CommandResources.TXT_T_REC, this);
setCurrent(mainMenuList);
}
public void handleEvent(int eventId) {
try {
switch (eventId) {
case CommandResources.EVENT_GO_CAM:
setCurrent(cameraForm);
break;
case CommandResources.EVENT_CAM:
this.photo("/test.jpeg");
break;
case CommandResources.EVENT_GO_REC:
setCurrent(recordForm);
break;
case CommandResources.EVENT_REC:
this.record("D://audio.mid");
break;
case CommandResources.EVENT_REC_PAUSE:
this.recordPause();
break;
case CommandResources.EVENT_REC_RESUME:
this.recordResume();
break;
case CommandResources.EVENT_REC_FIN:
this.recordCommit();
break;
case CommandResources.EVENT_BACK:
setCurrent(mainMenuList);
break;
case CommandResources.EVENT_EXIT:
midlet.exit(true);
break;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (MediaException e) {
e.printStackTrace();
}
}
public void setCurrent(Displayable dis) {
midlet.setCurrent(dis);
}
public void record(String path) throws IOException, MediaException {
if (recordPlayer == null) {
recordPlayer = Manager.createPlayer("capture://audio");
recordPlayer.realize();
}
recordControl = (RecordControl) recordPlayer
.getControl("RecordControl");
recordControl.reset();
if (recordControl != null) {
baos = new ByteArrayOutputStream();
recordControl.setRecordStream(baos);
// recordControl.setRecordLocation(path);
recordControl.startRecord();
recordPlayer.start();
}
}
public void recordPause() {
if (recordControl != null) {
System.out.println("PAUSE--SIZE = " + baos.size());
recordControl.stopRecord();
}
}
public void recordResume() {
if (recordControl != null) {
System.out.println("RESUME--SIZE = " + baos.size());
recordControl.startRecord();
}
}
public void recordCommit() throws IOException {
if (recordControl != null) {
recordControl.commit();
System.out.println("COMMIT--SIZE = " + baos.size());
}
}
public void photo(String path) throws IOException, MediaException {
if (videoPlayer == null) {
videoPlayer = Manager.createPlayer("capture://video");
videoPlayer.realize();
videoControl = (VideoControl) videoPlayer
.getControl("VideoControl");
if (videoControl != null) {
System.out.println("/** 已经得到了VideoControl!");
Item itemVideoWindow;
itemVideoWindow = (Item) videoControl.initDisplayMode(0, null);
// 在USE_GUI_PRIMITIVE模式下,默认是可见的,而在USE_DIRECT_VIDEO模式下,
// 默认是不可见的,需要通过方法setVisible(boolean visible)来设置
itemVideoWindow.setLayout(3);
cameraForm.append(itemVideoWindow);
videoPlayer.start();
}
videoControl.setVisible(true);
}
byte imageData[];
imageData = videoControl.getSnapshot("encoding="
+ CommandResources.IMG_TYPE);
// + "&width="
// + CommandResources.IMG_WIDTH + "&height="
// + CommandResources.IMG_HEIGHT);
if (imageData != null) {
System.out.println(imageData.length);
Image img = Image.createImage(imageData, 0, imageData.length);
}
}
public MainMenuList getMainMenuList() {
return mainMenuList;
}
public void setMainMenuList(MainMenuList mainMenuList) {
this.mainMenuList = mainMenuList;
}
public CameraForm getCameraForm() {
return cameraForm;
}
public void setCameraForm(CameraForm cameraForm) {
this.cameraForm = cameraForm;
}
public RecordForm getRecordForm() {
return recordForm;
}
public void setRecordForm(RecordForm recordForm) {
this.recordForm = recordForm;
}
}
以下是三个界面类:
package com.xiaoyu.record.view;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import com.xiaoyu.record.controller.GUIController;
import com.xiaoyu.record.common.CommandResources;
public class CameraForm extends Form {
private GUIController controller;
private Command cmdCam;
private Command cmdBack;
private Command cmdExit;
public CameraForm(String title, GUIController controller) {
super(title);
this.controller = controller;
cmdCam = new Command(CommandResources.TXT_C_CAM, Command.SCREEN, 1);
cmdBack = new Command(CommandResources.TXT_C_BACK, Command.BACK, 3);
cmdExit = new Command(CommandResources.TXT_C_EXIT, Command.EXIT, 4);
addCommand(cmdCam);
addCommand(cmdBack);
addCommand(cmdExit);
this.setCommandListener(new CameraFormListener());
}
private class CameraFormListener implements CommandListener {
public void commandAction(Command command, Displayable dis) {
if(command == cmdCam) {
controller.handleEvent(CommandResources.EVENT_CAM);
} else if(command == cmdBack) {
controller.handleEvent(CommandResources.EVENT_BACK);
} else if(command == cmdExit) {
controller.handleEvent(CommandResources.EVENT_EXIT);
}
}
}
}
package com.xiaoyu.record.view;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import com.xiaoyu.record.controller.GUIController;
import com.xiaoyu.record.common.CommandResources;;
public class MainMenuList extends List {
private GUIController controller;
private Command commandOk;
public MainMenuList(String title, GUIController controller) {
super(title, List.IMPLICIT);
this.controller = controller;
commandOk = new Command(CommandResources.TXT_C_OK,
Command.OK, 5);
this.addCommand(commandOk);
this.setCommandListener(new MainMenuListener());
this.append(CommandResources.TXT_I_CAMERA, null);
this.append(CommandResources.TXT_I_RECORD, null);
this.append(CommandResources.TXT_I_ABOUT, null);
this.append(CommandResources.TXT_I_EXIT, null);
}
private class MainMenuListener implements CommandListener {
public void commandAction(Command command, Displayable dis) {
if(command == commandOk || (command == List.SELECT_COMMAND)){
int index = ((List)dis).getSelectedIndex();
switch(index) {
case 0 :
controller.handleEvent(CommandResources.EVENT_GO_CAM);
break;
case 1 :
controller.handleEvent(CommandResources.EVENT_GO_REC);
break;
case 2 :
break;
case 3 :
controller.handleEvent(CommandResources.EVENT_EXIT);
break;
}
}
}
}
}
package com.xiaoyu.record.view;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import com.xiaoyu.record.common.CommandResources;
import com.xiaoyu.record.controller.GUIController;
public class RecordForm extends Form {
private GUIController controller;
private Command cmdRec;
private Command cmdPause;
private Command cmdBack;
private Command cmdExit;
private Command cmdFin;
private Command cmdResume;
public RecordForm(String title, GUIController controller) {
super(title);
this.controller = controller;
cmdRec = new Command(CommandResources.TXT_C_REC, Command.SCREEN, 1);
cmdPause = new Command(CommandResources.TXT_C_PAUSE, Command.SCREEN, 2);
cmdResume = new Command(CommandResources.TXT_C_RESUME, Command.SCREEN, 3);
cmdBack = new Command(CommandResources.TXT_C_BACK, Command.BACK, 4);
cmdExit = new Command(CommandResources.TXT_C_EXIT, Command.EXIT, 5);
cmdFin = new Command(CommandResources.TXT_C_REC_FIN, Command.SCREEN, 6);
addCommand(cmdRec);
addCommand(cmdPause);
addCommand(cmdBack);
addCommand(cmdExit);
addCommand(cmdFin);
addCommand(cmdResume);
this.setCommandListener(new RecordFormListener());
}
private class RecordFormListener implements CommandListener {
public void commandAction(Command command, Displayable dis) {
if(command == cmdRec) {
controller.handleEvent(CommandResources.EVENT_REC);
} else if(command == cmdPause) {
controller.handleEvent(CommandResources.EVENT_REC_PAUSE);
} else if(command == cmdBack) {
controller.handleEvent(CommandResources.EVENT_BACK);
} else if(command == cmdExit) {
controller.handleEvent(CommandResources.EVENT_EXIT);
} else if(command == cmdFin) {
controller.handleEvent(CommandResources.EVENT_REC_FIN);
} else if(command == cmdResume) {
controller.handleEvent(CommandResources.EVENT_REC_RESUME);
}
}
}
}
最后是主MIDLet类:
package com.xiaoyu.record.midlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.xiaoyu.record.controller.GUIController;
import com.xiaoyu.record.common.CommandResources;;;
public class RecordMIDLet extends MIDlet {
private Display display;
private static GUIController controller;
public RecordMIDLet() {
super();
display = Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
controller = null;
}
protected void pauseApp() {
if(display.getCurrent() == controller.getRecordForm()) {
controller.handleEvent(CommandResources.EVENT_REC_PAUSE);
}
this.notifyPaused();
}
protected void startApp() throws MIDletStateChangeException {
GUIController controller = new GUIController(this);
try {
controller.init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setCurrent(Displayable dis) {
display.setCurrent(dis);
}
public Displayable getCurrent() {
return display.getCurrent();
}
public Display getCurrentDisplay() {
return display;
}
public void exit(boolean flag) {
try {
this.destroyApp(flag);
this.notifyDestroyed();
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
}