1

In a child activity, I record some audio using MediaRecorder and have two buttons: The first one will start recording and second one is going back to its parent activity. However every time I hit go back button, it takes a long time to return to main activity's view. My code inside go back button's callback is:

public void onClick(View arg0) {        
        if (arg0.getId() == R.id.startRecord) 
        {
           StartRecording();
        } 

        else if (arg0.getId() == R.id.goBack) 
       {
       if(mediaRecorder!=null)
           {
             mediaRecorder.stop();
             mediaRecorder.release();
             mediaRecorder = null;
           }
       this.finish();

       }
   }

The parent activity's onCreate() method only initiate several buttons and set listener methods to them. I really cannot figure out why go back action will take a long time. A phenomenon worthy to mention is that if I don't record first but hit go back button first, it goes back really quick to the parent activity. The long responding time only happens after I record some audio. I do upload the recorded audio however I put the uploading in a AsyncTask task and I can get feedback when uploading work is done. After I see the feedback, and even wait for some time, the go back button still takes a long time to bring me back to main activity. Anyone has advices on this? Thanks!

Iam619
  • 795
  • 2
  • 12
  • 28
  • check logcat to see what logging messages are being written to the logs. Im guessing the stop() and release() of the mediaRecorder is blocking. – Akos Cz Aug 16 '12 at 18:29
  • i think moving the mediaRecorder.release to Ondestroy/Onresume could make it fast. Give it a try – nandeesh Aug 16 '12 at 18:29
  • Sounds like in the `onPostExecute()` you should redirect the user back to the main activity and then provide the alert of a successful upload. That way you could take the back button out of the equation, unless the user has the option to stay put. – jnthnjns Aug 16 '12 at 18:42
  • Yes..it is the stop() and release() that is blocking, since when I move them to onDestroy() of the child activity, the go back button can respond fast. However after I returned to the main activity, the main activity doesn't respond effectively to user's input (in my case, radio button choice). And the application quit after several seconds' frozen. However no message like "unfortunately the application is stopped" is given. I guess I have to stop and release the MediaRecorder when go back button is hit, right? How can I do this without waiting for a long time? Thanks for any help! – Iam619 Aug 16 '12 at 18:43
  • @Asok, After onPostExecute() updates the UI of the child activity, users have the option to record audio again or go back to main activity. So I guess I have to stay at the child activity and let user choose go back to restart the main activity... – Iam619 Aug 16 '12 at 18:50
  • Gotcha, you could override the back button. [Here is an example of how to do so](http://stackoverflow.com/a/3142471/1134705) – jnthnjns Aug 16 '12 at 18:53
  • @Asok, thank you very much for help! I have the onBackPressed() method override now which does the same as the go back button in the view. Currently, I removed stop(), release() and mediarecorder=null part (I don't do these actions at all), and the go back button works fast. Also, main activity works fine. and re-recording audio in the child activity works fine, too. So actions like mediaRecorder.stop(), mediaRecorder.release() and mediaRecorder = null are really not necessary? Or there is a potential problem if I don't do these actions? Thanks! – Iam619 Aug 16 '12 at 19:01
  • I think it does need those methods included to work properly. I am not entirely familiar with `MediaRecorder`. [According to the documentation it seems you should also call `reset()`](http://developer.android.com/reference/android/media/MediaRecorder.html) – jnthnjns Aug 16 '12 at 19:11
  • @Asok, I read the documents and tried reset() instead of top() and release(). However it still takes a while (like 5 to 10 seconds) to go back to main activity. Nothing happened if I don't re-initiate the media recorder though. I guess it might be ok? I'm not sure but currently I can only skip the re-initialization actions. Thank you very much for helping! – Iam619 Aug 17 '12 at 01:30

1 Answers1

1

You could override the back button, albeit from what I have seen it seems a lot of developers aren't to friendly to the idea.

@Override
public void onBackPressed() {
   mediaRecorder.stop();
   mediaRecorder.release();
   mediaRecorder.reset();
   mediaRecorder = null;
}

See if that works for you.


Better yet, something like this could be more useful, that way you could easily call the stop() method throughout with ease:

public void onClick(View arg0) {        
    if (arg0.getId() == R.id.startRecord) {
        StartRecording();
    } else if (arg0.getId() == R.id.goBack) {
        if(mediaRecorder!=null) {
        stopRecording();
    }
    this.finish();
}


public void stopRecording() throws IOException {
    mediaRecorder.stop();
    mediaRecorder.release();
    mediaRecorder.reset();
    mediaRecorder = null;
}

@Override
public void onBackPressed() {
   stopRecording();
}
jnthnjns
  • 8,962
  • 4
  • 42
  • 65