简体   繁体   English

呼叫记录失败,并出现java.lang.IllegalStateException

[英]Call Recording failed with java.lang.IllegalStateException

I am working with android.media.MediaRecorder for recording voice call, and got stuck at exception java.lang.IllegalStateException , please help 我正在使用android.media.MediaRecorder进行语音通话录音,并被卡在java.lang.IllegalStateException异常中,请帮忙

Code

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(SERVICE_TAG,"onStartCommand [flags: " + flags + ", startId: " + startId + "]");

        initMediaRecorder("onStartCommand");

        if(PojoClass.isRecording){
            return super.onStartCommand(intent, flags, startId);
        }else{
            try {
                //create Sound File.
                Recording = createOutputFile();
                //work for recording
                if(Recording != null){
                    iRecorder.reset();
                    //iRecorder.setAudioChannels(1);  //1 -> mono and 2-> Stereo
                    iRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    iRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //Line #82
                    iRecorder.setOutputFile(Recording.getAbsolutePath());
                    Log.d(MEDIA_RECORDER_TAG,"Recorder Set\nAudioChannels: 1 (mono)" +
                            "\nAudioSource: " + MediaRecorder.AudioSource.DEFAULT +
                            "\nAudioEncoder: " + MediaRecorder.AudioEncoder.DEFAULT +
                            "\nOutputFile: " + Recording.getAbsolutePath());

                    iRecorder.setOnErrorListener(this);
                    iRecorder.setOnInfoListener(this);
                    try {
                        iRecorder.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                        iRecorder.release();
                        iRecorder = null;
                    }

                    //start recording
                    iRecorder.start();
                    PojoClass.isRecording = true;
                    //Notify user that Call is being recorded.
                    NotifyUserOnRecording(true);
                }
            } catch(Exception e){
                e.printStackTrace();
                iRecorder.release();
                iRecorder = null;
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

LogCat logcat的

01-19 14:44:43.949    2191-2191/com.example.myapplication D/iRecordCall﹕ onCreate
01-19 14:44:43.949    2191-2191/com.example.myapplication D/MEDIA_RECORDER_TAG﹕ MediaRecorder is initialize at [onCreate]
01-19 14:44:43.951    2191-2191/com.example.myapplication D/iRecordCall﹕ onStartCommand [flags: 0, startId: 1]
01-19 14:44:43.951    2191-2191/com.example.myapplication D/MEDIA_RECORDER_TAG﹕ MediaRecorder is initialize at [onStartCommand]
01-19 14:44:43.961    2191-2191/com.example.myapplication E/MediaRecorder﹕ setAudioEncoder called in an invalid state(2)
01-19 14:44:43.961    2191-2191/com.example.myapplication W/System.err﹕ java.lang.IllegalStateException
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.media.MediaRecorder.setAudioEncoder(Native Method)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at com.example.myapplication.recorderservice.iRecordCall.onStartCommand(iRecordCall.java:82)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.app.ActivityThread.-wrap17(ActivityThread.java)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-19 14:44:43.963    2191-2191/com.example.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

The MediaRecorder has to be in the " DataSourceConfigured " state if you want to call 如果要调用, MediaRecorder必须处于“ DataSourceConfigured ”状态

iRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

So before this line you need to set the format of the file you want to generate, for example: 因此,在此行之前,您需要设置要生成的文件的格式,例如:

iRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

See also this link to documentation , especially the state diagram. 另请参见此文档链接,尤其是状态图。

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

相关问题 java.lang.IllegalStateException:无法获得表面 - java.lang.IllegalStateException: failed to get surface java.lang.IllegalStateException:无法内省类 - java.lang.IllegalStateException: Failed to introspect Class java.lang.IllegalStateException:无法执行 CommandLineRunner - java.lang.IllegalStateException: Failed to execute CommandLineRunner java.lang.IllegalStateException:无法加载ApplicationContext - java.lang.IllegalStateException: Failed to load ApplicationContext java.lang.IllegalStateException - java.lang.IllegalStateException 无法创建sessionFactory对象:java.lang.IllegalStateException HQL - Failed to create sessionFactory object: java.lang.IllegalStateException HQL SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException:无法加载ApplicationContext - SpringBoot + Mybatis + MySQL, java.lang.IllegalStateException: Failed to load ApplicationContext 测试 java.lang.IllegalStateException: 加载 ApplicationContext 失败 - Test java.lang.IllegalStateException: Failed to load ApplicationContext @WebMvcTest 失败,出现 java.lang.IllegalStateException:无法加载 ApplicationContext - @WebMvcTest fails with java.lang.IllegalStateException: Failed to load ApplicationContext Junit错误:java.lang.IllegalStateException:无法加载ApplicationContext - Junit error :java.lang.IllegalStateException: Failed to load ApplicationContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM