Hi all,

I'm running into some state problems with MediaPlayer.  I have so far
only seen this on one device (HTC Hero) running Android 1.5.  Stack
trace is:

java.lang.IllegalStateException
       at android.media.MediaPlayer.prepareAsync(Native Method)
       at
com.carboncrystal.ro.audio.SafeMediaPlayer.prepareAsync(Unknown
Source)
       at com.carboncrystal.ro.audio.a.a(Unknown Source)
       at
com.carboncrystal.ro.audio.SafeOnCompletionListener.onCompletion(Unknown
Source)

Now that's all pretty meaningless I know, but here's the relevant
code:

        public void onCompletion(MediaPlayer mp) {
                mp.stop();
                mp.prepareAsync();
        }

The "SafeMediaPlayer" referred to above is just my hack to track down
where the problem was (it's on a phone used by a friend in another
country, so difficult to monitor).

So basically the story is this:

In an effort to avoid reloading audio every time its used, I am
reusing the same MediaPlayer instance over and over.  In most cases, I
don't explicitly call "stop()" because most of the sounds are short
(sound effects that just wont squeeze into the tiny SoundPool cache)..
so I'm using an OnCompletionListener to let me know when the sound has
finished so I can prepare it for the next use.

Now.. according to the doco:

"Calling stop() stops playback and causes a MediaPlayer in the
Started, Paused, Prepared or PlaybackCompleted state to enter the
Stopped state.
Once in the Stopped state, playback cannot be started until prepare()
or prepareAsync() are called to set the MediaPlayer object to the
Prepared state again."

So ok, according to this I should be fine.. but it's a little unclear
to me from the state transition diagram (http://developer.android.com/
reference/android/media/MediaPlayer.html) whether the MediaPlayer
enters the PlaybackCompleted before or after the call is made to
onCompletion in the OnCompletionListener.

It sort of seems that when onCompletion is called, the MediaPlayer is
already in a Stopped state.  But who cares right.. calling stop() in a
stopped state should do nothing as it's a valid state.

Interestingly I don't always see the failure.. which tells me it may
be a timing thing.  In either case, does anyone have any relevant
experience with this?

Maybe there's a call to start() sneaking in between the call to stop()
and the call to prepareAsync() from another thread.. then again, I
would have thought this would give me a failure on the call to
start().. unless of course it is in a PlaybackCompleted state...
urgh.. my head hurts.

Thanks.

-- 
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

Reply via email to