The code below executes when some button is pressed. It plays an
audio file in res/raw and shows a horizontal progress bar while it is
running.
It works but has one quirky problem - If another dialog is opened
while this thread is running the ProgressBar does not clear at the end
(where it sets position=0). Then the NEXT time it runs it flashes the
ProgressBar on every update. After that buggy behavior it is okay
again on subsequent invocations.
I tried it without the Handler initially and it usually worked but had
similar but more frequent quirky behavior flashing the ProgressBar. So
the Handler seems to be required, but something is still not quite
right about what I am doing here.
Any suggestions...
class AudioPlayer
implements Runnable
{
int audioId;
ProgressBar progressBar;
Handler handler = new Handler();
int position;
AudioPlayer(int audioId, ProgressBar progressBar)
{
this.audioId = audioId;
this.progressBar = progressBar;
}
void play() {
new Thread(this).start();
}
private Runnable progressUpdater = new Runnable () {
public void run()
{ progressBar.setProgress(position); }
};
@Override
public void run() {
MediaPlayer player = MediaPlayer.create(context,
audioId);
int duration = player.getDuration();
try {
position = 0;
progressBar.setMax(duration);
player.start();
do {
try {
position = player.getCurrentPosition();
handler.post(progressUpdater);
Thread.sleep(200);
} catch (Exception ex) { break; }
} while (position < duration);
} finally {
position = 0;
handler.post(progressUpdater);
player.release();
}
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en