Android mediaplyer seekTo inside onPrepared

my problem seems to only happen on android 4.2.2

I go this way in an idle state → initialized state → prepareAsync () → and seekTo is called in onPrepared, but in this version of android the media player returns

"Attempted search at the end of the file: request = {int> 0}, durationMs = 0"

and start playing from the beginning, as it makes no sense so that I can catch this, nor the listener, he just writes this message to the log, I can not respond to it.

What's even weirder is if I call mediaPlayer.getDuration () in onPrepared (), it returns the correct value, not 0.

Do you think this is an intermediary mistake or is there a better place to call seekTo? or maybe a way to find out that seekTo failed? I would like to avoid periodically checking the current position if it is less than the desired position and try to trigger a search, since this approach has many different problems.

it's smooth streaming video content

+5
source share
3 answers

I am currently trying to find a solution to the same problem. The best thing I've come up with is the following.

In 4.2, I noticed that the following callbacks were received:

1) onVideoSizeChanged () - where height and width = 0
2) onPrepared ()
3) onVideoSizeChanged () - correct heights and widths

You cannot call seekTo in (1) because the player is not yet prepared.

, seekTo (2), " "

(3), MediaPlayer.start(), seekTo().

MediaPlayer mMediaPlayer = new MediaPlayer(); // + some initialisation code
boolean mVideoSizeIsSet = false;
boolean mMediaPlayerIsPrepared = false;

public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mMediaPlayerIsPrepared = true;

    if (mVideoSizeIsSet) {
        mMediaPlayer.seekTo();
    }

    mMediaPlayer.start()
}


public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.d(TAG, "onVideoSizeChanged called");

    if (width == 0 || height == 0) {
        Log.d(TAG, "invalid video width(" + width + ") or height(" + height + ")");
    } else {

        mVideoSizeIsSet = true;

        if (mMediaPlayerIsPrepared) {
            mMediaPlayer.seekTo();
        }
    }
}

( , , sdk, - ).

/ . 4.2. mMediaPlayer.start(), -, seekTo(), , . , onSeekComplete(), .

- , , .

+5

NuPlayer. NuPlayer , AwesomePlayer. , start(), . , 0, .

+2

:

418 status_t MediaPlayer::seekTo_l(int msec)
419 {
420     ALOGV("seekTo %d", msec);
421     if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED |  MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
422         if ( msec < 0 ) {
423             ALOGW("Attempt to seek to invalid position: %d", msec);
424             msec = 0;
425         } else if ((mDuration > 0) && (msec > mDuration)) {
426             ALOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
427             msec = mDuration;
428         }
429         // cache duration
430         mCurrentPosition = msec;
431         if (mSeekPosition < 0) {
432             getDuration_l(NULL);
433             mSeekPosition = msec;
434             return mPlayer->seekTo(msec);
435         }
436         else {
437             ALOGV("Seek in progress - queue up seekTo[%d]", msec);
438             return NO_ERROR;
439         }
440     }
441     ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
442     return INVALID_OPERATION;
443 }

getDuration(), mediaplayer . start() seekTo() seekTo() ().

0

All Articles