Strange behaviour with mediaplayer and seekTo

Posted by Mathias Lin on Stack Overflow See other posts from Stack Overflow or by Mathias Lin
Published on 2010-06-16T04:27:07Z Indexed on 2010/06/16 4:32 UTC
Read the original article Hit count: 290

I'm implementing a custom video player because I need custom video controls. I have an app with only one activity, which on startup shall start playing a video right away.

Now, the problem I have is:

I don't want the video to start from the beginning, but from a later position. Therefore I do a seekTo(16867). Since seekTo is asynchronous, I place the start call of the mediaplayer (player.start()) in the onSeekComplete of the onSeekCompleteListener.

The strange behaviour I experience though is that I can see/hear the video playing from the beginning for a few millisecs before it actually plays from/jumps to the position I seeked to. But - on the other hand - the Log output I call before the player.start returns the correct position 16867, where I seeked to.

Below is the relevant code section, the complete class is at http://pastebin.com/jqAAFsuX

(I'm on Nexus One / 2.2 StageFright)

private void playVideo(String url) {
    try {
        btnVideoPause.setEnabled(false);
        if (player==null) {
            player=new MediaPlayer();
            player.setScreenOnWhilePlaying(true);
        }
        else {
            player.stop();
            player.reset();
        }
        url = "/sdcard/myapp/main/videos/main.mp4";  // <--- just for test purposes hardcoded here now     
        player.setDataSource(url);
        player.setDisplay(holder);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setOnCompletionListener(this);
        player.setOnPreparedListener(this);

        player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            public void onSeekComplete(MediaPlayer mediaPlayer) {
                    Log.d("APP", "current pos... "+ player.getCurrentPosition() );
                    player.start();          // <------------------ start video on seek completed
                    player.setOnSeekCompleteListener(null);
                }
        });            
        player.prepareAsync();
    }
    catch (Throwable t) {
        Log.e(TAG, "Exception in btnVideoPause prep", t);
    }
}

public void onPrepared(MediaPlayer mediaplayer) {
    width=player.getVideoWidth();
    height=player.getVideoHeight();
    if (width!=0 && height!=0) {
        holder.setFixedSize(width, height);
        progressBar.setProgress(0);
        progressBar.setMax(player.getDuration());
        player.seekTo(16867);                   // <------------------ seeking to position
    }   
    btnVideoPause.setEnabled(true);
}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk