Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer seekTo does not work

I have a problem with seekTo() method from MediaPlayer. It seems it doesn't work at all! I followed other threads regarding this issue and tried different workarounds but still seekTo() does nothing! Here is my code:

private MediaPlayer mMediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
 ...
    final VideoView videoView = (VideoView)findViewById(R.id.videoView);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoView);
    final Uri video = Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.video_myopia);
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);
    videoView.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {

            mMediaPlayer = mp;

            System.out.println("CURRENT POSITION 0: " + mp.getCurrentPosition());
            mp.seekTo(3000);
            System.out.println("CURRENT POSITION 1: " + mp.getCurrentPosition());
            mp.start();
            System.out.println("CURRENT POSITION 2: " + mp.getCurrentPosition());
        }
    });

The System.out.println() shows the following output:

  • POSITION 0: 0
  • POSITION 1: 3000
  • POSITION 2: 3000

(note: I also tried to start() before seekTo() and then start() again after the seekTo() method)

so after the mp.start() line the mp.getCurrentPosition() says that it is at 3000 but the problem is that it's not! The video starts from the beginning. In each situation that I tried seek, after seekTo(ms) and start() the video starts from the beginning no matter what.

I must mention that I also used mp.setOnSeekCompleteListener() to know when the seek finishes. In this callback I tried to start() the video again, but it always starts from the beginning.

Can someone help me please? I will be grateful for any suggestion :). Thanx!

like image 972
Ispas Claudiu Avatar asked May 07 '14 10:05

Ispas Claudiu


1 Answers

I discovered that my videos were not seekable, and that's why every time I used seekTo it made my video start from the beginning. In order to solve the problem I converted them with H.264

like image 145
Ispas Claudiu Avatar answered Sep 27 '22 17:09

Ispas Claudiu