为什么在停止录音时会出现IllegalStateException异常?

6

我想用麦克风录制音频并保存音频文件。开始录制时运作正常,但是当我尝试停止录制时,模拟器会出现强制关闭错误。堆栈跟踪:

01-09 18:16:59.075: E/AndroidRuntime(831): FATAL EXCEPTION: main
01-09 18:16:59.075: E/AndroidRuntime(831): java.lang.IllegalStateException
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.media.MediaRecorder.stop(Native Method)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.example.voice.recorder.MainActivity.StopRecording(MainActivity.java:45)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.example.voice.recorder.MainActivity$1.onClick(MainActivity.java:76)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.view.View.performClick(View.java:3511)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.view.View$PerformClick.run(View.java:14105)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.os.Handler.handleCallback(Handler.java:605)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.os.Looper.loop(Looper.java:137)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-09 18:16:59.075: E/AndroidRuntime(831):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:16:59.075: E/AndroidRuntime(831):  at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-09 18:16:59.075: E/AndroidRuntime(831):  at dalvik.system.NativeStart.main(Native Method)

在MediaRecorder.stop()上出错了; 这是我尝试停止录制的方式:

public void StopRecording() throws IOException{
    recorder.stop();
    recorder.reset();
    recorder.release();
    recorder = null;
}

我如何开始录制:

public class MainActivity extends Activity {
    MediaRecorder recorder;
public void StartRecording(){
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile("/sdcard/sample.3gp");
    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}   

我该如何调用这个方法:

            if (!tv.getText().equals("Recording...")){
                tv.setText("Recording...");
                tv.setTextColor(Color.RED);
                record.setImageResource(R.drawable.microphone_icon_pressed);
                StartRecording();

            }else{
                tv.setText("Click the button to start recording");
                record.setImageResource(R.drawable.microphone_icon);
                tv.setTextColor(Color.BLACK);
                try {
                    StopRecording();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

我的清单文件中有这两个权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

开始录制正常,但停止录制不正常。有人知道代码哪里出了问题吗?


请不要在问题中重复标签。我已经将其编辑掉了。 - Simon
你能否为你的代码添加更多的上下文信息,例如你是如何/在哪里声明recorder的?(如果它是BroadcastReceiver的非静态成员,你可能会遇到这些问题,例如)。 - Michael
你是否完成了 音频捕获指南 中的第3、4、5、6步骤? - Stan
我编辑了我的问题,并附上了我开始录制的代码。 - Simon
我不仅指的是你如何启动录音机,还包括你如何在哪里声明它。这些变量和方法属于哪种对象类型? - Michael
1个回答

1
你的录音机显然没有处于录音状态。你需要确保它已经成功启动。因为在调用stop()之前调用IllegalStateException时会发生异常。如果抛出RuntimeException,请在stop()块中添加代码以删除输出文件。
请参见MediaRecorder.java
  /**
     * Stops recording. Call this after start(). Once recording is stopped,
     * you will have to configure it again as if it has just been constructed.
     * Note that a RuntimeException is intentionally thrown to the
     * application, if no valid audio/video data has been received when stop()
     * is called. This happens if stop() is called immediately after
     * start(). The failure lets the application take action accordingly to
     * clean up the output file (delete the output file, for instance), since
     * the output file is not properly constructed when this happens.
     *
     * @throws IllegalStateException if it is called before start()
     */
    public native void stop() throws IllegalStateException;

我建议您在需要连续启动和停止时,不要释放录音机对象,直到应用程序关闭。根据以下流程,在onCreate()/onResume()中创建录音机,并在onPause/onDestroy()中释放。

enter image description here


但是OP的代码包括对start()方法的调用,那么这个答案如何帮助解决问题呢? - Marvin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接