Thanks, but actually I "think" I have solved the problem.

Although I'm not 100% sure how or why this exception was thrown, it
seems the MediaPlayer is not in the state I expect it to be.  My
solution is to wrap the MediaPlayer instance and implement my own
state indicator.  So when the MediaPlayer transitions state, I
manually set a state variable in my wrapper class (by implementing the
various listeners available to capture state transitions).  Then
before I execute any methods on the MediaPlayer instance (start/stop/
pause etc) I check the state to make sure we are where we should be,
and if not handle accordingly.

Unfortunately I haven't been able to test again on the HTC Hero as
yet, but this has solved a similar error I was getting on another
device.


On Sep 18, 12:42 pm, Jason <[email protected]> wrote:
> Anyone?.. Bueller...
>
> On Sep 18, 5:20 am, Jason <[email protected]> wrote:
>
>
>
> > Hi all,
>
> > I'm running into some state problems withMediaPlayer.  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(MediaPlayermp) {
> >                 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 sameMediaPlayerinstance 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 aMediaPlayerin 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 theMediaPlayerobject 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 theMediaPlayer
> > enters the PlaybackCompleted before or after the call is made to
> > onCompletion in the OnCompletionListener.
>
> > It sort of seems that when onCompletion is called, theMediaPlayeris
> > 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