MediaExtractor译文

MediaExtractor

官网描述是:MediaExtractor facilitates extraction of demuxed, typically encoded, media data from a data source. added in API level 16。 public final class MediaExtractor
extends Object.
译文:MediaExtractor便于从数据源中提取复用的,典型编码的媒体数据。 在api 16时候加入到sdk的。
其一般的用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(...);
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (weAreInterestedInThisTrack) {
extractor.selectTrack(i);
}
}
ByteBuffer inputBuffer = ByteBuffer.allocate(...)
while (extractor.readSampleData(inputBuffer, ...) >= 0) {
int trackIndex = extractor.getSampleTrackIndex();
long presentationTimeUs = extractor.getSampleTime();
...
extractor.advance();
}

extractor.release();
extractor = null;

在使用该类的时候可能需要使用到Manifest.permission.INTERNET权限,当数据源为网络数据时。

概要

  1. 嵌套子类
类名 描述
MediaExtractor.CasInfo Describes the conditional access system used to scramble a track. (描述用于对轨道进行加扰的条件访问系统。)
MediaExtractor.MetricsConstants 度量标准常量
  1. 常量
常量名 值类型 描述
SAMPLE_FLAG_ENCRYPTED int The sample is (at least partially) encrypted(采样加密), see also the documentation for MediaCodec.queueSecureInputBuffer(int, int, MediaCodec.CryptoInfo, long, int) 2 (0x00000002)
SAMPLE_FLAG_PARTIAL_FRAME int This indicates that the buffer only contains part of a frame, and the decoder should batch the data until a buffer without this flag appears before decoding the frame.(这表明缓冲区只包含帧的一部分,并且解码器应该对数据进行批处理,直到没有该标志的缓冲区在解码帧之前出现。) 4 (0x00000004)
SAMPLE_FLAG_SYNC int The sample is a sync sample (or in MediaCodec’s terminology it is a key frame.)(该示例是同步样本(或者在MediaCodec的术语中,它是一个关键帧)。) 1 (0x00000001)
SEEK_TO_CLOSEST_SYNC int If possible, seek to the sync sample closest to the specified time(如果可能的话,寻找最接近指定时间的同步采样) 2 (0x00000002)
SEEK_TO_NEXT_SYNC int If possible, seek to a sync sample at or after the specified time(如果可能的话,在指定的时间或之 寻找同步样本。) 1 (0x00000001)
SEEK_TO_PREVIOUS_SYNC int If possible, seek to a sync sample at or before the specified time(如果可能的话,在指定的时间或之 寻找同步样本。) 0 (0x00000000)
  1. 公共构造器
    MediaExtractor()

  2. 公共方法

方法名 返回类型 描述
advance() boolean Advance to the next sample.(前进到下一个采样)
getAudioPresentations(int trackIndex) List Get the list of available audio presentations for the track.(获取该曲目的可用音频演示列表。)
getCachedDuration() long Returns an estimate of how much data is presently cached in memory expressed in microseconds.(返回目前在内存中缓存的数据量,以微秒为单位表示。)
getCasInfo(int index) MediaExtractor.CasInfo Retrieves the information about the conditional access system used to scramble a track.(检索有关用于加密曲目的条件访问系统的信息。)
getDrmInitData() DrmInitData Extract DRM initialization data if it exists(提取DRM初始化数据(如果存在))
getMetrics() PersistableBundle Return Metrics data about the current media container.(返回有关当前媒体容器的度量标准数据。)
getPsshInfo() Map Get the PSSH info if present.(获取PSSH信息,如果存在。)
getSampleCryptoInfo(MediaCodec.CryptoInfo info) boolean If the sample flags indicate that the current sample is at least partially encrypted, this call returns relevant information about the structure of the sample data required for decryption.(如果样本标志指示当前样本至少部分加密,则此调用返回有关解密所需的样本数据结构的相关信息。)
getSampleFlags() int Returns the current sample’s flags.(返回当前采样的标志。)
getSampleSize() long Returns the current sample’s presentation size
getSampleTime() long Returns the current sample’s presentation time in microseconds.(以微秒为单位返回当前样本的呈现时间。)
getSampleTrackIndex() int Returns the track index the current sample originates from (or -1 if no more samples are available)(返回当前样本来源的轨道索引(如果没有更多样本可用,则返回-1))
getTrackCount() int Count the number of tracks found in the data source.(计算数据源中找到的曲目数量。)
getTrackFormat(int index) MediaFormat Get the track format at the specified index.(获取指定索引处的曲目格式。)
hasCacheReachedEndOfStream() boolean Returns true iff we are caching data and the cache has reached the end of the data stream (for now, a future seek may of course restart the fetching of data).(如果我们正在缓存数据并且缓存已到达数据流的末尾(现在,未来的查找当然可能会重新开始提取数据),则返回true。)
readSampleData(ByteBuffer byteBuf, int offset) int Retrieve the current encoded sample and store it in the byte buffer starting at the given offset.(检索当前编码样本并将其存储在从给定偏移量开始的字节缓冲区中。)
release() void Make sure you call this when you’re done to free up any resources instead of relying on the garbage collector to do this for you at some point in the future.(确保你在完成任务时释放任何资源,而不是依赖垃圾回收器在将来的某个时刻为你做这件事。)
seekTo(long timeUs, int mode) void All selected tracks seek near the requested time according to the specified mode.(根据指定的模式,所有选定的曲目都会在请求的时间附近寻找。)
selectTrack(int index) void Subsequent calls to readSampleData(ByteBuffer, int), getSampleTrackIndex() and getSampleTime() only retrieve information for the subset of tracks selected.(随后对readSampleData(ByteBuffer,int),getSampleTrackIndex()和getSampleTime()的调用仅检索所选轨道子集的信息。)
setDataSource(AssetFileDescriptor afd) void Sets the data source (AssetFileDescriptor) to use.(设置要使用的数据源(AssetFileDescriptor))
setDataSource(Context context, Uri uri, Map headers) void Sets the data source as a content Uri.(将数据源设置为内容Uri。)
setDataSource(FileDescriptor fd) void Sets the data source (FileDescriptor) to use.((设置要使用的数据源(FileDescriptor)))
setDataSource(MediaDataSource dataSource) void Sets the data source (MediaDataSource dataSource) to use.(设置要使用的数据源(MediaDataSource))
setDataSource(FileDescriptor fd, long offset, long length) void Sets the data source (FileDescriptor) to use.(设置要使用的数据源(MediaDataSource))
setDataSource(String path) void Sets the data source (file-path or http URL) to use.(设置要使用的数据源(文件路径或http URL))
setDataSource(String path, Map headers) void Sets the data source (file-path or http URL) to use.(设置要使用的数据源(文件路径或http URL))
setMediaCas(MediaCas mediaCas) void Sets the MediaCas instance to use.(设置要使用的MediaCas实例。)
unselectTrack(int index) void Subsequent calls to readSampleData(ByteBuffer, int), getSampleTrackIndex() and getSampleTime() only retrieve information for the subset of tracks selected.(随后对readSampleData(ByteBuffer,int),getSampleTrackIndex()和getSampleTime()的调用仅检索所选轨道子集的信息。)