4

I'm using Android's MediaPlayer class to start playing a song from some offset. After specifying this offset in code, I play the song expecting to hear it from that offset position but rather I hear it play from the beginning. Please note the following code:

    MediaPlayer mplayer = new MediaPlayer();
    mplayer.reset();
    try {
        // For example's sake, specify some known song path
        mplayer.setDataSource("//media/external/audio/media/1");
        // call blocking prepare()
        mplayer.prepare();
    } catch (Exception e) {
    // Keep try/catch simple for example's sake
        e.printStackTrace();
        return;
    }

    // seekTo is an asynchronous operation. Set it's complete callback to play the song once seekTo has completed
    mplayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
        @Override
        public void onSeekComplete(MediaPlayer mp) {
            // Start the song 30 seconds in
            mp.start();
        }
    });
    // Seek to 30 seconds into the song
    mplayer.seekTo(30000);

This code sample runs without errors in the emulator (tested against Android 1.6, 2.1, and 2.2); however, when run on some handsets, the song will play from the beginning. I know for sure this happens on my Droid Incredible (android 2.2). Also note that LogCat will always have some output from the MediaPlayer class stating the song's position has indeed been set to some offset. I've debugged this for hours and scoured forums and still have no solution. Please help.

jdgilday
  • 866
  • 7
  • 21

1 Answers1

2

In case you haven't found it on your own by now, this is a buffering problem (issue documented here). Seeking to a non-buffered position results in playing from the beginning instead. The proposed solutions are found here (not the most elegant, but about all there is to do).

Community
  • 1
  • 1
Cameron
  • 3,098
  • 1
  • 22
  • 40
  • Thanks for your input. I don't think these problems are the same. The bug you've linked to deals with streaming media and claims to be remedied in 2.2. The bug I'm describing seems to only affect HTC phones (as far as I can tell). – jdgilday Jan 20 '11 at 02:13
  • They claimed to have fixed in in 2.2, but if you read in the comments most people still have the problem. I have an Incredible and still have this problem. Have you tested it on non-HTC phones? – Cameron Jan 21 '11 at 15:52
  • On my HTC Evo, if I seek to a buffered or unbuffered position, it restarts the stream from the beginning and I see a StageFright error in the logs. – HenryAdamsJr Jan 28 '11 at 21:27