Android MediaExtractor seek() accuracy with MP3 au

2019-05-21 12:20发布

问题:

I am having trouble getting reasonable accuracy on seek() with Android MediaExtractor. For some files, such as this one, I find significant inaccuracies (about 3 seconds in this case.) I have the following flow:

  1. call setDataSource() and selectTrack()
  2. call seekTo()
  3. call play()

In this case, it seeks to an incorrect position, but reports the correct position. If I then seek to another position, it continues to be off by the same margin. However, if I seek to 0, it corrects itself, and then will seek to the correct location in the file.

Is this simply a limitation / bug in MediaExtractor? Does anyone have a fix / workaround to get more accuracy? I would prefer not to start playback at position 0 before seeking, but I suppose that is one option.

Some other notes:

  • Android MediaPlayer shows the same behavior.
  • This happens whether I select MediaExtractor.SEEK_TO_PREVIOUS_SYNC or SEEK_TO_CLOSEST_SYNC
  • I see the same problem on KitKat and Lollipop, on the emulator and physical devices.
  • A quick analysis of one failed file showed "Wrong number of MPEG data bytes specified in Xing header," but I got the same result for files that played fine.