简体   繁体   English

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

[英]Android IllegalStateException when recording audio on some devices

I want my program to record audio for 10 seconds and then stop and store my record in file storage, everything works fine on my Nexus 4 and Galaxy S 5, but when i test it in Galaxy S3 it crushes and raise error 我希望我的程序记录音频10秒钟,然后停止并将记录存储在文件存储中,在Nexus 4和Galaxy S 5上一切正常,但是当我在Galaxy S3中对其进行测试时,它会崩溃并引发错误

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)

First i thought the error is in the file name, I'm declaring it like this: 首先,我认为错误出在文件名中,我这样声明:

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

Then i make a record like this: 然后我做这样的记录:

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");
    }


}

Then i call the "StartRecording" method like this: 然后,我像这样调用“ StartRecording”方法:

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. you are not supposed to call mRecorder.start(); 您不应该调用mRecorder.start(); when mRecorder.prepare(); 当mRecorder.prepare();时 fails. 失败。

this is what caused the Illegal State Exception to be thrown. 这就是引发“非法状态异常”的原因。

  1. Not all devices support all the encoding formats. 并非所有设备都支持所有编码格式。 try changing these mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 尝试更改这些mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); and mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 和mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

change the format and Encoder to a file format and encoding format your s3 device supports. 将格式和编码器更改为s3设备支持的文件格式和编码格式。

for a start, change your encoding setting to default settings as follows and try if it works. 首先,请按照以下说明将编码设置更改为默认设置,然后尝试使用。 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM