Search code examples
androidandroid-mediacodecmediaextractor

Android: Get Current Position Using MediaExtractor & MediaCodec


I'm creating my own implementation of Media Player using the MediaCode, MediaExtractor and AudioTrack. I need to perform distortions on the decoded mp3 samples I read from locally stored audio files- hence the custom implementation. For this reason I need to implement my own seekTo and getCurrentPosition functions. However, I'm not sure how I would go about implementing the latter.

I could set up a listener to increment the position while playing but this sounds like a terrible idea and probably not accurate. What is the best way to do it? I am looking to support API level 16 or greater.


Solution

  • The answer came from using the MediaExtractor. Once the extractor has read sample data via the extractor.readSampleData(...) method, a sample has been loaded.

    A subsequent call to extractor.getSampleTime() provides the current time in microseconds. Dividing that value by 1,000 gives the milisecond value. I store this variable as its read and it's the source for subsequent calls to my custom player's getCurrentPosition() method.