Strange behavior with media planner and seekTo

I use a custom video player because I need custom video controls. I have an application with only one action, which at start should immediately start playing the video.

Now I have a problem:

I don’t want the video to start from the very beginning, but from a later position. So I do seekTo (16867). Since seekTo is asynchronous, I put the start up call of the media player (player.start ()) in onSeekComplete for onSeekCompleteListener.

The strange behavior I'm experiencing is that I can see / hear the video being played from the very beginning for a few milliseconds before it starts playing / jumps to the position I was looking for. But, on the other hand, the Log output, which I call before playing player.start, returns the correct position 16867 that I was aiming for.

Below is the corresponding section of code, the full class is at http://pastebin.com/jqAAFsuX

(I'm on a 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);
}
+5
source share

All Articles