Android MediaRecorder在无效状态4下启动失败

时间:2013-10-24 06:43:51

标签: android multithreading android-mediarecorder

我尝试录制音频,只录制音频,我在Android 4.1.2上收到此错误,但在4.3上没有:

以下是代码:

private void startRecord(){
    on_record = true;
    final boolean exists = (new File(Environment.getExternalStorageDirectory() + File.separator + NOTAY)).exists();
        if (!exists) {new File(Environment.getExternalStorageDirectory() + File.separator + "Test").mkdirs();}

        final boolean existAud = (new File(android.os.Environment.getExternalStorageDirectory() + File.separator + NOTAY + File.separator + "Audio")).exists();
        if (!existAud) {new File(Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator + "Audio").mkdirs();}

        final EditText editTitle= (EditText) findViewById(R.id.editTitle);
        final String title = editTitle.getText().toString();
        final String audioName = getDate(System.currentTimeMillis()).replaceAll(" ", "_");
        currentAudioPath =    Environment.getExternalStorageDirectory().getAbsolutePath()
                            + File.separator
                            + "Test"
                            + File.separator
                            + "Audio"
                            + File.separator
                            + System.currentTimeMillis() + "-" + audioName + ".3gpp";

        if(title.length() == 0)
            editTitle.setText(audioName);

        Recorder = new MediaRecorder();

        Recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        Recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        Recorder.setOutputFile(currentAudioPath);


        try {
            Recorder.prepare();
        } catch (IOException e) {

        }

        Recorder.start();

    }

    private void saveRecord(){

        Recorder.stop();
        Recorder.release();
        Recorder = null;
        on_record = false;

        currentAudioPath = NO_AUDIO;

    }

这里有日志:

10-24 08:34:02.777: E/MediaRecorder(13491): start called in an invalid state: 4
10-24 08:34:02.777: D/AndroidRuntime(13491): Shutting down VM
10-24 08:34:02.777: W/dalvikvm(13491): threadid=1: thread exiting with uncaught exception (group=0x40e5b440)
10-24 08:34:02.823: E/AndroidRuntime(13491): FATAL EXCEPTION: main
10-24 08:34:02.823: E/AndroidRuntime(13491): java.lang.IllegalStateException
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.media.MediaRecorder.start(Native Method)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.androtest.audio.startRecord(AudioActivity.java:357)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.androtest.audio.onOptionsItemSelected(AudioActivity.java:307)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.app.Activity.onMenuItemSelected(Activity.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.view.View.performClick(View.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.view.View$PerformClick.run(View.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.os.Handler.handleCallback(Handler.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.os.Handler.dispatchMessage(Handler.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.os.Looper.loop(Looper.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at android.app.ActivityThread.main(ActivityThread.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-24 08:34:02.823: E/AndroidRuntime(13491):    at dalvik.system.NativeStart.main(Native Method)

我已经阅读了数百个关于此错误的帖子,但我找不到任何可以解决我的问题...

4 个答案:

答案 0 :(得分:5)

我遇到了同样的错误并通过修复文件路径解决了这个问题。因此,如果您的状态4无效,检查您是否设置了输出文件路径以及它是否真的存在

答案 1 :(得分:4)

好的,我发现我的问题是什么。 这样:

final String audioName = getDate(System.currentTimeMillis()).replaceAll(" ", "_");

返回包含此字符的字符串:“:” 替换为:

final String audioName = (getDate(System.currentTimeMillis()).replaceAll(" ", "_")).replaceAll(":", "-");

解决了我的问题。

答案 2 :(得分:0)

我遇到了同样的问题,然后意识到输出文件的路径不正确。纠正路径解决了我的问题。

答案 3 :(得分:0)

您需要在初始化Recorder后调用reset

     Recorder = new MediaRecorder();
            Recorder.reset();
            Recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            Recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            Recorder

.setOutputFile(currentAudioPath);


        try {
            Recorder.prepare();
        } catch (IOException e) {

        }

        Recorder.start();