在某些设备上录制音频时出现Android IllegalStateException

时间:2014-10-02 07:51:55

标签: java android

我希望我的程序录制音频10秒然后停止并将我的记录存储在文件存储中,一切正常在我的Nexus 4和Galaxy S 5上运行,但是当我在Galaxy S3中测试时它会破坏并引发错误< / p>

10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/AudioCaptureDemo﹕ prepare() failed 10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/MediaRecorder﹕ start called in an invalid state: 4 10-02 02:13:44.942 1279-1279/com.taptester.tappapp D/AndroidRuntime﹕ Shutting down VM 10-02 02:13:44.942 1279-1279/com.taptester.tappapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a12ba8) 10-02 02:13:44.952 1279-1279/com.taptester.tappapp E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.taptester.tappapp, PID: 1279 java.lang.IllegalStateException at android.media.MediaRecorder.start(Native Method) at com.taptester.tappapp.MainActivity.startRecording(MainActivity.java:203) at com.taptester.tappapp.MainActivity.access$300(MainActivity.java:62) at com.taptester.tappapp.MainActivity$6.onFinish(MainActivity.java:825) at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

首先我认为错误在文件名中,我是这样声明的:

public MainActivity() {
    mFileName = Environment.getExternalStorageDirectory() + File.separator
            + Environment.DIRECTORY_DCIM + File.separator + "MyMemo.3gp";
    //Environment.getExternalStorageDirectory().getAbsolutePath();
    //mFileName += "/MyMemo.3gp";
}

然后我做这样的记录:

private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

    try {
        mRecorder.prepare();
        mRecorder.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }


}

然后我打电话给&#34; StartRecording&#34;像这样的方法:

else if(command.equals("2")) {

                    startRecording();

                    Toast.makeText(getApplicationContext(), "Start recording...",
                            Toast.LENGTH_SHORT).show();

                    CountDownTimer start = new CountDownTimer(timer, 1000) {

                        @Override
                        public void onTick(long l) {
                            Toast.makeText(getApplicationContext(), "Recording!!!",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onFinish() {
                            stopRecording();

                        }
                    }.start();

1 个答案:

答案 0 :(得分:1)

  1. 你不应该打电话给mRecorder.start();当mRecorder.prepare();失败。
  2. 这是导致非法国家例外被抛出的原因。

    1. 并非所有设备都支持所有编码格式。 尝试更改这些mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 和mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    2. 将格式和编码器更改为s3设备支持的文件格式和编码格式。

      首先,将您的编码设置更改为默认设置,如下所示,并尝试它是否有效。     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);