Hello All
I am facing recording issues in Blackberry storm series. The code is working fine in other models. But in 9500 series I am getting the problem. When I tested the application, I am always getting zero bytes. Also most of the times, Application is getting hanged because of Player component.
Below is the code I am using for this
Code:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
public class VoiceRecorderComponent extends Thread {
private Player _player;
private RecordControl _rcontrol;
private ByteArrayOutputStream _output;
private byte _data[];
private int choice=-1;
public static final long MAX_RECORDING_TIME = 30000;
public static final int START_RECORDER = 0;
public static final int PAUSE_RECORDER = 1;
public static final int DESTROY_RECORDER = 2;
public static final int RESUME_RECORDER = 3;
public VoiceRecorderComponent() {
_player=null;
_rcontrol=null;
_output=null;
initRecorder();
}
public void setCommand(int choice) {
this.choice = choice;
switch (choice) {
case START_RECORDER:
start();
break;
case PAUSE_RECORDER:
pauseRecording();
break;
case RESUME_RECORDER:
resumeRecording();
break;
default:
break;
}
}
public int getCommand() {
return choice;
}
public void initRecorder() {
boolean _error = false;
try {
if (null == _player) {
_player = Manager
.createPlayer("capture://audio?encoding=audio/amr");
_player.prefetch();
_player.realize();
_rcontrol = (RecordControl) _player.getControl("RecordControl");
_output = new ByteArrayOutputStream();
_rcontrol.setRecordStream(_output);
}
System.err.println("Player Instance Created: "+_player);
} catch (IOException error) {
_error = true;
} catch (IllegalArgumentException error) {
_error = true;
} catch (IllegalStateException error) {
_error = true;
} catch (SecurityException error) {
_error = true;
} catch (MediaException error) {
_error = true;
} catch (Throwable error) {
error.printStackTrace();
_error = true;
}
}
public void run() {
try {
recordingScreen.updateFocusForBottomPanel(false);
_player.start();
_rcontrol.startRecord();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public byte[] getVoiceNote() {
return _data;
}
public void pauseRecording() {
//recordingScreen.updateFocusForBottomPanel(true);
_rcontrol.stopRecord();
}
public void resumeRecording() {
try {
//recordingScreen.updateFocusForBottomPanel(false);
_rcontrol.startRecord();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public byte[] getVoiceBufferForPreview() {
//_rcontrol.stopRecord();
try {
_output.flush();
_data = _output.toByteArray();
System.err.println("_data = _output.toByteArray();"+_data+" "+_data.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return _data;
}
public byte[] getVoiceBuffer() {
_data = null;
try {
if (null != _player && null != _rcontrol) {
_rcontrol.stopRecord();
_rcontrol.commit();
_output.flush();
_data = _output.toByteArray();
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return _data;
}
public void destroyPlayer() {
try {
if (null != _player) {
_player.stop();
freeAudioBuffer();
_player.close();
_player = null;
_rcontrol = null;
}
} catch (MediaException me) {
} catch (IllegalStateException me) {
} catch (Exception ex) {
}
_player = null;
_rcontrol = null;
}
public void freeAudioBuffer() {
try {
if (null != _output) {
_output.close();
_output = null;
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getVoiceBufferForTimeDisplay() {
try {
if (null != _player && null != _rcontrol) {
_rcontrol.stopRecord();
}
} catch (Exception e) {
e.printStackTrace();
}
return _output.size();
}
private void clearMemory() {
try {
System.gc();
} catch (Throwable error) {
}
}
}