As a followup for future readers of this post - I did get the example shown to work very nicely with a little refinement. First there are problems with creating multiple MediaPlayer objects so best practice seems to be to only create one and reuse it with reset(). I did this with a single thread java.util.concurrent.ExecutorService. Secondly you have to pay attention to the state model for MediaPlayer. One issue (possible SDK bug?) I found is that if you call reset() when prepare() has not returned the Activity stops responding with an ANR and there is no call to MediaPlayer's Error listener if you have set one.
On Aug 26, 5:47 pm, jotobjects <[email protected]> wrote: > 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

