我有一个按钮来控制开始录制和停止录制。首先不录制当前,开始录制。如果当前录制,停止录制,然后将文件转到下一个活动。但现在开始录制工作正常,但停止录制时,应用程序崩溃显示以下错误:
java.lang.RuntimeException:stop failed.at android.media.MediaRecorder.stop(Native Method)
有时,单击停止录制时不会崩溃,但是当我回到此视频录制活动时。它再次崩溃,同时显示相同的错误。
以下是我实现按钮的方法。有关详细信息,我的刻录机prepare()
位于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
}
}
}
所以我的问题是如何制作一个按钮来正确控制开始和停止录制过程?有关信息,我不想使用ToggleButton
,我需要一个普通的按钮。
提前致谢。
编辑:
我在RuntimeExeception
到mRecorder.start
和mRecorder.stop
如下,但应用程序仍然在第三次崩溃时单击按钮(录制时应重新开始)。
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();
}
}
}
以下是我在应用崩溃时获得的堆栈跟踪:
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)
答案 0 :(得分:1)
我发现问题是,我在停止录制时没有释放Camera和MediaRecorder。所以我在下面解决了这个问题,希望可以帮助其他人:
@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();
}