Android development MediaExTractor explanation

Foreword

  MediaExtractor letter means multimedia extractor, it’s sound in Android Video development is mainly responsible for extracting information and data streams in video or audio. You can create your own video player or audio player in cooperation with MediaCodec.

Get track

  In a multimedia video, multiple data streams can be included. We need to obtain the required data track number first, and then follow-up operations. The following code shows how to obtain the track.

private void< /span> trackDemo(){

mFile
= new File(getExternalCacheDir(),"demo.mp4" );
if (!mFile.exists()){
Log.e(TAG,
"mp4 file does not exist");
return;
}
MediaExtractor extractor
= new MediaExtractor();// Instance a MediaExtractor
try {
extractor.setDataSource(mFile.getAbsolutePath());
//Set Add MP4 file path
} catch (IOException e) {
e.printStackTrace();
}
int count = extractor.getTrackCount();//Get the number of tracks
Log.e(TAG, "Number of tracks = "+count);
for (int i = 0; i ){
MediaFormat mediaFormat
= extractor.getTrackFormat(0);
Log.e(TAG, i
+"Numbered channel format = "+mediaFormat.getString(MediaFormat.KEY_MIME));
}

}

The above code has been commented very clearly. Attention! In actual projects, it is recommended to put these operations in the thread .

Result:

2019-08-19 16:49:18.514 17742-17742/demo.yt.com.demo E/Test Demo : count = 2

2019-08-19 16:49:18.515 17742-17742/demo.yt.com.demo E/Test Demo: 0 Number channel format = video/avc
2019-08-19 16:49:18.515 17742-17742/demo.yt.com.demo E/Test Demo: 1 Number channel format = video/avc

Extract video data information

From Get data information in MediaFormat

In the code for getting tracks above, we can see the use of< span style="color: #000000;">MediaFormat can get the encoding format of the current track. In addition to getting the encoding format, we can also get a lot of other information, here we will list them one by one:

Extract data stream

private void trackDemo(){

mFile
= new File(getExternalCacheDir(),"demo.mp4" );
if (!mFile.exists()){
Log.e(TAG,
"mp4 file does not exist");
return;
}
MediaExtractor extractor
= new MediaExtractor();// Instance a MediaExtractor
try {
extractor.setDataSource(mFile.getAbsolutePath());
//Set Add MP4 file path
} catch (IOException e) {
e.printStackTrace();
}
int count = extractor.getTrackCount();//Get the number of tracks
Log.e(TAG, "Number of tracks = "+count);
for (int i = 0; i ){
MediaFormat mediaFormat = extractor.getTrackFormat(0);
Log.e(TAG, i
+"Numbered channel format = "+mediaFormat.getString(MediaFormat.KEY_MIME));
}

}

2019-08-19 16:49:18.514 17742-17742/demo.yt.com.demo E/Test Demo: count = 2

2019-08-19 16:49:18.515 17742-17742/demo.yt.com.demo E/Test Demo: 0 Number channel format = video/avc
2019-08-19 16:49:18.515 17742-17742/demo.yt.com.demo E/Test Demo: 1 Number channel format = video/avc

Leave a Comment

Your email address will not be published.