相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

    I didn't use Android for that. As I recall I used ffmpeg to convert the videos and also to find if the videos are seekable. Or, I also think that I've searched on the internet to see what type of codec Androis support – Ispas Claudiu Feb 22, 2015 at 10:30 @IspasClaudiu, can you please tell me how did you find out your videos were not seekable? – Ahmed Ashraf Aug 28, 2016 at 13:16 @AhmedAshrafG I don;t recall the software that I used for that but a video tool or ffmpeg I suppose can tell you this kind of information – Ispas Claudiu Aug 29, 2016 at 9:26 Converting a video to h.264 does not make them seekable. I have an mp4 that is not seekable - at least not using the default video player app on Android. A lot of testing clearly indicates that the way the video is encoded will determine if Android can make it seekable. A video can be seekable using video players like VLC but not seek using Android's default player. Getting the video encoded using MediaCodec to create a seekable mp4 is a lot more tricky than it appears. Oddly, the default camera app on Android can create an mp4 that is seekable. – Johann Jul 5, 2021 at 10:03

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

     
    推荐文章