androidandroid-mediacodecmediaextractor

MediaExtractor extract from bundle of multiple audio files


I am attempting to use MediaExtractor and MediaCodec to play AAC (m4a) packed together in a single file.

The file structure is like this......

HEADER

track1.m4a

track2.m4a

etc...

Where the header contains the offsets and lengths of the audio files packed below it.

I am attempting to then create MediaExtractors for these individual files like this:

AssetFileDescriptor afd = context.getAssets().openFd(bundleFile);

long startOffset = clip.byteOffset;
long length = clip.assetLength;


Log.i(TAG, "open: Loading from bundle startOffset: " + startOffset + " length: " + length);

extractor = new MediaExtractor();

extractor.setDataSource(afd.getFileDescriptor(), startOffset, length);
extractor.selectTrack(0);
extractor.seekTo(offsetUS, MediaExtractor.SEEK_TO_NEXT_SYNC);

afd.close();

MediaFormat fmt = extractor.getTrackFormat(0);
String mimeType = fmt.getString(MediaFormat.KEY_MIME);

decoder = MediaCodec.createDecoderByType(mimeType);

This code works when I am simply loading an m4a file from assets, and use afd.getStartOffset() and afd.getLength() However when I try to extract the bytes I want out of the middle as above, I get

java.io.IOException: Failed to instantiate extractor 
      at android.media.MediaExtractor.setDataSource(Native Method)  

So it seems clear to me that this may not be possible. I suspect that MediaExtractor uses the file extension to parse the header or something like this, and doesn't know to interpret this data as m4a.

Is there another way to accomplish this goal of having a single bundled file for multiple aac files? I was thinking I could feed MediaCodec manually with InputFileStream or something similar...

Thanks!


Solution

  • Have you tried setting absolute offset?

    extractor.setDataSource(afd.getFileDescriptor(), afd.getStartOffset() + startOffset, length);