5

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);
}
Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
  • btw: I experience the same when using the VideoView of the SDK, instead of my own media player+surface implementation. I'm also wondering why VideoView doesn't provide an setOnSeekCompletionListener, although it provides a seekTo. Isn't the seekTo async as well? – Mathias Conradt Jun 17 '10 at 01:35
  • Filed a bug report at http://code.google.com/p/android/issues/detail?id=9135 – Mathias Conradt Jun 17 '10 at 03:51
  • Any luck with workarounds so far? I know 2 years on but this is now bothering me in my app – Vrashabh Irde Oct 25 '12 at 07:09
  • Do you know what streams were in your mp4? Can you confirm this is H264 baseline / AAC? – robd Feb 17 '14 at 16:37

1 Answers1

4

Since I also haven't recieved any reply from the Android developer list and found similar issues on the web, I conclude it's a bug. I filed a bug report at http://code.google.com/p/android/issues/detail?id=9135

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192