0

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 Answers1

4

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.

mstorsjo
  • 12,983
  • 2
  • 39
  • 62
  • Do you happen to know how can I bundle a demuxer of my own? Can you give me guide/tutorial or simple explanation? – Atanas Bozhanin Dec 01 '14 at 08:09
  • You need to write an MPEG2 TS demuxer yourself and include it, or find a suitable existing library with a suitable license and include it in your project. – mstorsjo Dec 01 '14 at 09:40
  • 1
    simply use ffmpeg, it is quite easy to reuse splitter from there and wrap it into MediaExtractor API – Marlon Dec 02 '14 at 08:55
  • 1
    @ AtanasBozhanin, you could have marked the answer as @mstorsjo made a lot of efforts (finding code in Android source) and he helped with an additional comment. – gregoiregentil May 07 '16 at 05:00