【问题标题】:Call Recording failed with java.lang.IllegalStateException通话录音因 java.lang.IllegalStateException 而失败
【发布时间】:2016-01-19 17:32:52
【问题描述】:

我正在与android.media.MediaRecorder 合作录制语音通话,但遇到异常java.lang.IllegalStateException,请帮助

代码

@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

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)

【问题讨论】:

    标签: java android mediarecorder


    【解决方案1】:

    MediaRecorder 必须处于“DataSourceConfigured”状态才能调用

    iRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    

    所以在这一行之前你需要设置你要生成的文件的格式,例如:

    iRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    

    另请参阅documentation 的链接,尤其是状态图。

    【讨论】:

    • @0x0nosugure 谢谢,它对我有用,但在iRecorder.start() 上也显示了相同的异常,因为 MediaRecorder:启动失败:-38
    • @iSandeep - 您将“iRecorder.prepare()”放在了 try-catch-block 中。你能确定它是否成功吗?
    • @0x0nosugure 是的,它是成功的
    • @iSandeep - 有一些关于 SO 的问题,例如摄像头被阻止的地方,例如 here
    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多