MediaPlayer ends after a pause

I recently noticed a very strange problem when MediaPlayer was playing an mp3 file. I run this code ( mPlayer is MediaPlayer):

 Log.d(TAG, "Pausing"); try { mPlayer.pause(); Log.d(TAG, "Paused"); } catch (IllegalStateException e) { Log.w(TAG, "exception pausing player"); } 

The strange thing is that if I am close to the end of the audio file, the player will send a notification of completion to my OnCompletionListener shortly after the completion of the above code. (I did not record exactly how close I should be, but it is on the order of 1/4 of a second.) For example, here is a typical logcat output when this happens:

 05-27 17:23:43.439: DEBUG/Player(266): Pausing 05-27 17:23:43.487: DEBUG/Player(266): Paused 05-27 17:23:43.838: WARN/Player(266): Audio completed (state=PAUSED) 

Please note that the warning line (registered from my OnCompletionListener ) occurs 300 ms after the pause() call returns!

As a result, the media player enters the PlaybackCompleted state when I do not expect this. This hides the behavior of my code (as well as start() , which restarts from the very beginning, rather than losing the last bit of the file).

This happened on emulators from 1.6 to 2.3 and at least on one device running 2.2. Does anyone know about this problem and what to do with it?

+4
source share
1 answer

The media player works in a separate stream, and you need to wait until the ui thread and the media player stream are synchronized. Thus, the delay is normal. Perhaps the player is really completing the file because your pause team reached the player too late. Try what happens with a longer audio file.

Another problem that may arise is a different code search outside the track after the pause command. What is your listener's code?

+1
source

All Articles