我正在制作一个记录未压缩(波形格式)音频的应用程序。我正在使用this class实际录制音频。目前,我的应用程序可以正常录制(我可以播放文件),但是,当我单击按钮停止录制时,该应用程序挂起10秒钟左右,没有日志输出或任何生命迹象。终于解决了,将大量错误转储到日志中,更新了UI等。我正在使用AsyncTasks尝试避免这种情况,但是它不起作用。这是我的代码:

//Called on clicks of the record button. rar is the instance of RehearsalAudioRecorder

private OnClickListener RecordListener = new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.d("Record","Click");
            if (recording){
                new stopRecordingTask().execute(rar,null,null);
                startStop.setText("Record");
                statusBar.setText("Recording Finished, ready to Encode");

            }else{
                recording = true;
                new startRecordingTask().execute(rar,null,null);
                startStop.setText("Stop");
                statusBar.setText("Recording Started");
            }

        }

    };
private class startRecordingTask extends AsyncTask<RehearsalAudioRecorder,Void,Void>{
        @Override
        protected Void doInBackground(RehearsalAudioRecorder... rs) {
            RehearsalAudioRecorder r = rs[0];
            r.setOutputFile("/sdcard/rarOut.wav");
            r.prepare();
            r.start();
            return null;
        }

    }
    private class stopRecordingTask extends AsyncTask<RehearsalAudioRecorder,Void,Void>{
        @Override
        protected Void doInBackground(RehearsalAudioRecorder... rs) {
            RehearsalAudioRecorder r = rs[0];
            r.stop();
            r.reset();
            return null;
        }

    }


在Logcat中,我总是得到这样的输出,这让我很困惑。我不知道是什么原因引起的(我正在记录RehearsalAudioRecorder类,并且通过单击按钮可以正确启动/停止它。此输出发生在按钮单击和正确的stop()方法调用的日志输出之后)

12-19 11:59:11.172: ERROR/AudioRecord-JNI(22662): Unable to retrieve AudioRecord object, can't record
12-19 11:59:11.172: ERROR/uk.ac.cam.tfmw2.steg.RehearsalAudioRecorder(22662): Error occured in updateListener, recording is aborted
12-19 11:59:11.172: ERROR/uk.ac.cam.tfmw2.steg.RehearsalAudioRecorder(22662): stop() called on illegal state: STOPPED
12-19 11:59:11.172: ERROR/AudioRecord-JNI(22662): Unable to retrieve AudioRecord object, can't record
12-19 11:59:11.172: ERROR/uk.ac.cam.tfmw2.steg.RehearsalAudioRecorder(22662): Error occured in updateListener, recording is aborted
12-19 11:59:11.172: ERROR/uk.ac.cam.tfmw2.steg.RehearsalAudioRecorder(22662): stop() called on illegal state: ERROR
12-19 11:59:11.172: ERROR/AudioRecord-JNI(22662): Unable to retrieve AudioRecord object, can't record
12-19 11:59:11.172: ERROR/uk.ac.cam.tfmw2.steg.RehearsalAudioRecorder(22662): Error occured in updateListener, recording is aborted
12-19 11:59:11.172: ERROR/uk.ac.cam.tfmw2.steg.RehearsalAudioRecorder(22662): stop() called on illegal state: ERROR
... 10 or more times


我整天都在摆弄,但我什么都没走,任何输入将不胜感激。

更新资料
我已将AsyncTasks替换为Threads,但仍然无法正常工作,当我单击record时,该应用程序完全挂起,尽管Log指示主线程中没有任何事件。仍然完全陷入困境。

最佳答案

您已授予应用程序RECORD_AUDIO权限?

您还应该更改RehearsalAudioRecorder中的catch块以记录实际的异常,而不是仅记录现在记录的“ updateListener中发生错误,记录被中止”。

10-04 10:45
查看更多