I have a button to control start recording and stop recording.First not recording currently,start recording.If recording currently,stop recording,then intent the file to next activity.But now start recording is working fine,but when stop recording,the app crash showing below error:
java.lang.RuntimeException: stop failed.at android.media.MediaRecorder.stop(Native Method)
Sometimes,it not crashing when click to stop recording,but when I go back to this video recording activity.It crash again,showing the same error as well.
Here is how I implement the button..For more information,my recorder is prepare()
in surfaceCreated()
.
boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
videoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isRecording){
stopRecording();
}else{
startRecording();
}
}
}
private void startRecording() {
Log.d("Video","start recording");
isRecording = true;
mRecorder.start();
}
private void stopRecording() {
Log.d("Video","stop recording");
isRecording = false;
if (null != recorder) {
try{
mRecorder.stop();
mRecorder.reset();
mRecorder.release();
}
catch(RuntimeException ex){
//Ignore
}
}
}
So my question is how to make a button to control the start and stop recording process correctly?And for information,I not interested to use ToggleButton
,I need a normal button.
Thanks in advance.
EDIT:
I at the RuntimeExeception
to the mRecorder.start
and mRecorder.stop
like below,but the app still crashed at the third time clicked the button(when the recording should start again).
private void startRecording() {
Log.d("Video","start recording");
isRecording = true;
try {
mRecorder.start();
}catch (RuntimeException e){
e.printStackTrace();
}
}
private void stopRecording() {
Log.d("Video","stop recording");
isRecording = false;
if(mRecorder != null) {
try {
mRecorder.stop();
mRecorder.reset();
mRecorder.release();
}catch (RuntimeException e){
e.printStackTrace();
}
}
}
Here is the stack trace I got when the app is crashed:
10-19 16:59:03.605 1671-3096/? W/ActivityManager: Spurious death for ProcessRecord{c3a33c7 0:com.ssapp/u0a71}, curProc for 18644: null
10-19 16:59:03.610 1671-1692/? W/WindowManager: Attempted to add application window with unknown token Token{465cf6e ActivityRecord{88a76e9 u0 com.ch.ssapp/.chat.activity.ChatActivity t446 f}}. Aborting.
10-19 16:59:03.610 1671-1692/? W/WindowManager: Token{465cf6e ActivityRecord{88a76e9 u0 /.chat.activity.ChatActivity t446 f}} already running, starting window not displayed. Unable to add window -- token Token{465cf6e ActivityRecord{88a76e9 u0 /.chat.activity.ChatActivity t446 f}} is not valid; is your activity running?
10-19 16:59:03.610 1671-1692/? W/WindowManager: view not successfully added to wm, removing view
10-19 16:59:03.610 1671-1692/? W/WindowManager: Exception when adding starting window
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{aab303e V.E...... R.....ID 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:424)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:350)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
at com.android.server.policy.PhoneWindowManager.addStartingWindow(PhoneWindowManager.java:2359)
at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7840)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
I found the problem is,I didn't release the Camera and MediaRecorder when stop recording.So I solve this below,hope can help someone else:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
videoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isRecording){
stopRecording();
}else{
startRecording();
}
}
}
private void startRecording() {
Log.d("Video","start recording");
mCamera = Camera.open();
if(prepareRecorder()){
mRecorder.start();
isRecording = true;
}else{
if (mRecorder != null) {
// clear recorder configuration
mRecorder.reset();
mRecorder.release();
mRecorder = null;
mCamera.lock();
}
}
}
private void stopRecording() {
Log.d("Video","stop recording");
try{
mRecorder.stop();
}catch (RuntimeException e) {
// RuntimeException is thrown when stop() is called immediately after start().
// In this case the output file is not properly constructed ans should be deleted.
Log.d(TAG, "RuntimeException: stop() is called immediately after start()");
}
if (mRecorder != null) {
// clear recorder configuration
mRecorder.reset();
mRecorder.release();
mRecorder = null;
mCamera.lock();
}
mCamera.lock();
isRecording = false;
releaseCamera();
}