Reputation: 795
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!
Upvotes: 1
Views: 276
Reputation: 8925
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();
}
Upvotes: 1