title: Android app hanging, sometimes until Force Close / Wait dialog appears
body: <p>I'm making an app that records uncompressed (wav format) audio. I'm using <a href="http://rehearsalassist.svn.sourceforge.net/viewvc/rehearsalassist/android/releases/RehearsalAssistant_0_8_2/src/urbanstew/RehearsalAssistant/RehearsalAudioRecorder.java?revision=90&amp;view=markup" rel="nofollow">this class</a> 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:</p>
  
  <pre><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&lt;RehearsalAudioRecorder,Void,Void&gt;{
          @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&lt;RehearsalAudioRecorder,Void,Void&gt;{
          @Override
          protected Void doInBackground(RehearsalAudioRecorder... rs) {
              RehearsalAudioRecorder r = rs[0];
              r.stop();
              r.reset();
              return null;
          }
  
      }
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I've been fiddling with this all day and I'm not getting anywhere, any input would be greatly appreciated.</p>
  
  <p><strong>Update</strong>
  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.</p>