MediaExtractor returns wrong number of tracks?

1.2k views Asked by At

I'm trying to build a player that plays mpeg2-ts stream with 2 audio tracks using MediaCodec and MediaExtractor. When I set the URL to the extractor: extractor.setDataSource(URL) int the Logcat I can see that the framework has found the 2 audio tracks:2 aduio tracks and 1 video track

But afterwards I call:

int trackCount = extractor.getTrackCount();
for (int i = 0; i < track_count; i++){
    format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith("video/")) ...
    if (mime.startsWith("audio/")) ...
}

trackCount aways equals 2(1 audio track & 1 video track). What am I doing wrong?

1

There are 1 answers

4
mstorsjo On BEST ANSWER

You're not doing anything wrong - it just seems that the MPEG2TSExtractor class (the actual implementation behind MediaExtractor for mpeg2 ts files) only supports one audio stream and one video stream.

See e.g. the init method in https://android.googlesource.com/platform/frameworks/av/+/1a9c3954a/media/libstagefright/mpeg2ts/MPEG2TSExtractor.cpp (lines 156-193). So if you need to demux any mpeg2 ts streams with multiple audio streams, you basically need to bundle a demuxer of your own.