Reputation: 225
I'm creating an app that records audio. If it records for a long time, then it takes it a few moments to write the header for it (wave file). I want to show a progress bar while that happends but it doesnt show. It seems as if the UI is stuck while running the method.
this is the code for when you click "stop recording":
ProgressBar Bar = (ProgressBar)findViewById(R.id.Bar);
TextView loadingTxt = (TextView)findViewById(R.id.loading);
Bar.setVisibility(View.VISIBLE);
loadingTxt.setAlpha(1f);
AppLog.logString("Stop Recording");
stopRecording();
Toast.makeText(getApplicationContext(), R.string.stoppedRecording, Toast.LENGTH_LONG).show();
Bar.setVisibility(View.GONE);
loadingTxt.setAlpha(0f);
The methode stopRecording() takes a while to run, and I want the progress bar to show during that time. How can I fix it?
Upvotes: 1
Views: 1951
Reputation: 3026
You need to use a AsyncTask here.. It does heavy operation in background thread since doInBackground method is called in separate thread.
final ProgressBar Bar = (ProgressBar)findViewById(R.id.Bar);
final TextView loadingTxt = (TextView)findViewById(R.id.loading);
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
Bar.setVisibility(View.VISIBLE);
loadingTxt.setAlpha(1f);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Toast.makeText(getApplicationContext(), R.string.stoppedRecording, Toast.LENGTH_LONG).show();
Bar.setVisibility(View.GONE);
loadingTxt.setAlpha(0f);
}
@Override
protected Void doInBackground(Void... params) {
stopRecording();
return null;
}
}.execute();
Upvotes: 2