Essa classe grava um vídeo com a camera do celular, só que ele não ta pegando os eventos do PlayerListener, pra quando clicar em gravar remover o comando e adicionar o comando parar e assim por diante, abaixo o código:
[code]
package hello;
import javax.microedition.lcdui.;
import javax.microedition.media.;
import javax.microedition.media.control.;
import java.io.;
public class VideoRecordingForm extends Form implements CommandListener, PlayerListener {
private final static Command CMD_BACK = new Command("Voltar", Command.BACK, 2);
private final static Command CMD_RECORD = new Command("Gravar", Command.SCREEN, 1);
private final static Command CMD_STOP_RECORD = new Command("Parar Gravação", Command.SCREEN, 1);
private final static Command CMD_SHOW_RECORDED = new Command("Exibir Gravação", Command.SCREEN, 1);
private final static Command CMD_MUTE = new Command("Mute", Command.SCREEN, 1);
private final static Command CMD_UNMUTE = new Command("UnMute", Command.SCREEN, 1);
private final static Command CMD_HIGHEST_VOLUME = new Command("Aumentar Volume", Command.SCREEN, 1);
private final static Command CMD_MEDIUM_VOLUME = new Command("Diminuir Volume", Command.SCREEN, 1);
private short currentState = -1;
private static final short CUSTOM_SHOW_RECORDED = 1;
private static final short CUSTOM_OK_TO_RECORD = 2;
ByteArrayOutputStream output = null;
private Player player;
private VideoControl videoControl;
private VideoCanvas aVideoCanvas;
String contentType = null;
private VolumeControl volumeControl;
private MobileVideoApp parentMidlet = null;
private VideoRecordingForm self = null;
private Form parentForm = null;
private VideoRecordingThread videoThread;
protected VideoRecordingForm(String in, MobileVideoApp parentMidlet_, Form parentForm_) {
super(in);
this.parentMidlet = parentMidlet_;
this.parentForm = parentForm_;
self = this;
initComponents();
}
public void initComponents() {
addCommand(CMD_BACK);
setCommandListener(this);
(new CameraThread()).start();
}
public void commandAction(Command c, Displayable d) {
if (c == CMD_BACK) {
releaseResources();
parentMidlet.getDisplay().setCurrent(parentForm);
}
else if (c == CMD_RECORD) {
videoThread = new VideoRecordingThread();
videoThread.start();
}
else if (c == CMD_STOP_RECORD) {
videoThread.stopRecord();
}
}
private void showCamera() {
try {
releaseResources();
player = Manager.createPlayer("capture://video");
player.addPlayerListener(this);
player.realize();
videoControl = (VideoControl)player.getControl("VideoControl");
aVideoCanvas = new VideoCanvas();
aVideoCanvas.initControls(videoControl, player);
aVideoCanvas.addCommand(CMD_BACK);
aVideoCanvas.setCommandListener(this);
parentMidlet.getDisplay().setCurrent(aVideoCanvas);
player.start();
contentType = player.getContentType();
}catch(Exception e) {
e.printStackTrace();
}
}
private void releaseResources() {
if ( player != null ) {
try {
player.stop();
player.close();
}catch(Exception e){}
}
}
public void playerUpdate(Player player, String event, Object evtData)
{
if (event == PlayerListener.STARTED && currentState != CUSTOM_SHOW_RECORDED) {
aVideoCanvas.addCommand(CMD_RECORD);
aVideoCanvas.removeCommand(CMD_MUTE);
aVideoCanvas.removeCommand(CMD_UNMUTE);
aVideoCanvas.removeCommand(CMD_HIGHEST_VOLUME);
aVideoCanvas.removeCommand(CMD_MEDIUM_VOLUME);
}
else if ( event == PlayerListener.RECORD_STARTED ) {
aVideoCanvas.removeCommand(CMD_RECORD);
aVideoCanvas.addCommand(CMD_STOP_RECORD);
}
else if ( event == PlayerListener.RECORD_STOPPED ) {
aVideoCanvas.removeCommand(CMD_STOP_RECORD);
aVideoCanvas.addCommand(CMD_SHOW_RECORDED);
}
else if ( event == PlayerListener.END_OF_MEDIA && currentState == CUSTOM_SHOW_RECORDED) {
currentState = CUSTOM_OK_TO_RECORD;
aVideoCanvas.addCommand(CMD_RECORD);
aVideoCanvas.removeCommand(CMD_STOP_RECORD);
aVideoCanvas.removeCommand(CMD_SHOW_RECORDED);
(new CameraThread()).start();
}
}
class CameraThread extends Thread {
public void run() {
showCamera();
}
}
class VideoRecordingThread extends Thread {
RecordControl rc;
public void run() {
recordVideo();
recordDummy();
}
public void recordVideo(){
try {
rc = (RecordControl)player.getControl("RecordControl");
if ( rc == null ) {
//Mostrar Alerta de erro
return;
}
output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
}
catch (Exception e) {}
}
public void stopRecord(){
try{
if ( rc != null ) {
rc.stopRecord();
rc.commit();
}
}catch (Exception e){}
}
public void recordDummy() {
try {
InputStream is = getClass().getResourceAsStream("/video/dummyrecord.mpg");
output = new ByteArrayOutputStream();
int len = is.available();
byte[] bytes = new byte[len];
is.read(bytes);
output.write(bytes, 0, bytes.length);
aVideoCanvas.removeCommand(CMD_RECORD);
aVideoCanvas.addCommand(CMD_STOP_RECORD);
contentType = "video/mpeg";
}catch(Exception e){}
}
}
}[/code]
Justamente aqui ele não recebe:
[quote]
public void playerUpdate(Player player, String event, Object evtData)
{
if (event == PlayerListener.STARTED && currentState != CUSTOM_SHOW_RECORDED) {
aVideoCanvas.addCommand(CMD_RECORD);
aVideoCanvas.removeCommand(CMD_MUTE);
aVideoCanvas.removeCommand(CMD_UNMUTE);
aVideoCanvas.removeCommand(CMD_HIGHEST_VOLUME);
aVideoCanvas.removeCommand(CMD_MEDIUM_VOLUME);
}
else if ( event == PlayerListener.RECORD_STARTED ) {
aVideoCanvas.removeCommand(CMD_RECORD);
aVideoCanvas.addCommand(CMD_STOP_RECORD);
}
else if ( event == PlayerListener.RECORD_STOPPED ) {
aVideoCanvas.removeCommand(CMD_STOP_RECORD);
aVideoCanvas.addCommand(CMD_SHOW_RECORDED);
}
else if ( event == PlayerListener.END_OF_MEDIA && currentState == CUSTOM_SHOW_RECORDED) {
currentState = CUSTOM_OK_TO_RECORD;
aVideoCanvas.addCommand(CMD_RECORD);
aVideoCanvas.removeCommand(CMD_STOP_RECORD);
aVideoCanvas.removeCommand(CMD_SHOW_RECORDED);
(new CameraThread()).start();
}
}[/quote]
:roll: