Android app hanging, sometimes until Force Close / Wait dialog appears

Posted by fredley on Stack Overflow See other posts from Stack Overflow or by fredley
Published on 2010-12-19T12:11:42Z Indexed on 2010/12/29 22:53 UTC
Read the original article Hit count: 285

Filed under:
|

I'm making an app that records uncompressed (wav format) audio. I'm using this class to actually record the audio. Currently, my application records fine (I can play the file), however when I click the button to stop the recording, the app hangs for 10 seconds or so, with no log output or any signs of life. Finally it comes round, dumps a load of errors into the log, updates the UI etc. I'm using AsyncTasks to try and avoid this kind of thing but it's not working. Here's my code:

//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;
        }

    }

In Logcat, I always get output like this, which has me stumped. I have no idea what's causing it (I'm logging the RehearsalAudioRecorder class, and it's being started/stopped correctly by the button clicks. This output occurs after the log output for the button click and correct stop() method call)

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

I've been fiddling with this all day and I'm not getting anywhere, any input would be greatly appreciated.

Update I've replace the AsyncTasks with Threads, still doesn't work, the app completely hangs when I click record, despite the fact the Log indicates there's nothing going on in the main thread. Still completely stumped.

© Stack Overflow or respective owner

Related posts about android

Related posts about freeze