public final class MediaMuxer
extends Object
| java.lang.Object | |
| android.media.MediaMuxer | |
MediaMuxer有助于混合基本流。 目前支持mp4或webm文件作为输出和最多一个音频和/或一个视频基本流。 MediaMuxer不支持复用B帧。
它通常是这样使用的:
MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
// More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
// or MediaExtractor.getTrackFormat().
MediaFormat audioFormat = new MediaFormat(...);
MediaFormat videoFormat = new MediaFormat(...);
int audioTrackIndex = muxer.addTrack(audioFormat);
int videoTrackIndex = muxer.addTrack(videoFormat);
ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
boolean finished = false;
BufferInfo bufferInfo = new BufferInfo();
muxer.start();
while(!finished) {
// getInputBuffer() will fill the inputBuffer with one frame of encoded
// sample from either MediaCodec or MediaExtractor, set isAudioSample to
// true when the sample is audio data, set up all the fields of bufferInfo,
// and return true if there are no more samples.
finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
if (!finished) {
int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
}
};
muxer.stop();
muxer.release();
Nested classes |
|
|---|---|
class |
MediaMuxer.OutputFormat 定义输出格式。 |
Public constructors |
|
|---|---|
MediaMuxer(String path, int format) 构造函数。 |
|
公共方法(Public methods) |
|
|---|---|
int |
addTrack(MediaFormat format) 添加具有指定格式的曲目。 |
void |
release() 确保你在完成任务时释放任何资源,而不是依赖垃圾回收器在将来的某个时刻为你做这件事。 |
void |
setLocation(float latitude, float longitude) 在输出文件中设置并存储地理数据(经度和纬度)。 |
void |
setOrientationHint(int degrees) 设置输出视频播放的方向提示。 |
void |
start() 启动混合器。 |
void |
stop() 停止混合器。 |
void |
writeSampleData(int trackIndex, ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo) 将编码的样本写入复用器。 |
Protected methods |
|
|---|---|
void |
finalize() 当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
MediaMuxer (String path, int format)
构造函数。 创建写入指定路径的媒体复用器。
| 参数(Parameters) | |
|---|---|
path |
String: The path of the output media file. |
format |
int: The format of the output media file. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if path is invalid or format is not supported. |
IOException |
if failed to open the file for write. |
也可以看看:
int addTrack (MediaFormat format)
添加具有指定格式的曲目。
下表汇总了Android版本中对特定格式键的支持。 标有'+:'的键是必需的。
| OS Version(s) | MediaFormat keys used for |
||
|---|---|---|---|
| All Tracks | Audio Tracks | Video Tracks | |
JELLY_BEAN_MR2 |
+: KEY_MIME |
+: KEY_SAMPLE_RATE,+: KEY_CHANNEL_COUNT,+: codec-specific dataAAC |
+: KEY_WIDTH,+: KEY_HEIGHT,no KEY_ROTATION, use setOrientationHint().mp4,+: codec-specific dataAVC, MPEG4 |
KITKAT |
|||
KITKAT_WATCH |
|||
LOLLIPOP |
as above, plus +: codec-specific dataVorbis & .webm |
||
LOLLIPOP_MR1 |
|||
M |
as above, plusKEY_BIT_RATEAAC |
||
N |
as above, plusKEY_BIT_RATEMPEG4,KEY_HDR_STATIC_INFO#, .webm,KEY_COLOR_STANDARD#,KEY_COLOR_TRANSFER#,KEY_COLOR_RANGE#,+: codec-specific dataHEVC, codec-specific dataVP9 |
||
| 笔记: |
|||
| 请注意,该轨道的编解码器专用数据必须使用此方法指定。 此外,编解码器专用数据不得通过 |
|||
下表总结了Android版本中对容器的编解码器支持:
| OS Version(s) | Codec support | ||
|---|---|---|---|
| MP4 | WEBM | ||
JELLY_BEAN_MR2 |
AAC,NB-AMR,WB-AMR,H.263,MPEG-4,AVC (H.264) |
Not supported | |
KITKAT |
|||
KITKAT_WATCH |
|||
LOLLIPOP |
Vorbis,VP8 |
||
LOLLIPOP_MR1 |
|||
M |
|||
N |
as above, plusHEVC (H.265) |
as above, plusVP9 |
|
| 参数(Parameters) | |
|---|---|
format |
MediaFormat: The media format for the track. This must not be an empty MediaFormat. |
| 返回(Returns) | |
|---|---|
int |
The track index for this newly added track, and it should be used in the writeSampleData(int, ByteBuffer, MediaCodec.BufferInfo). |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if format is invalid. |
IllegalStateException |
if muxer is in the wrong state. |
void setLocation (float latitude,
float longitude)
在输出文件中设置并存储地理数据(经度和纬度)。 此方法应在start()之前start() 。 如果输出格式为MUXER_OUTPUT_MPEG_4 ,地理数据存储在udta框中,并且其他输出格式将被忽略。 地理数据根据ISO-6709标准进行存储。
| 参数(Parameters) | |
|---|---|
latitude |
float: Latitude in degrees. Its value must be in the range [-90, 90]. |
longitude |
float: Longitude in degrees. Its value must be in the range [-180, 180]. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
If the given latitude or longitude is out of range. |
IllegalStateException |
If this method is called after start(). |
void setOrientationHint (int degrees)
设置输出视频播放的方向提示。
这个方法应该在start()之前start() 。 调用此方法不会在muxer生成文件时旋转视频帧,但如果输出格式为MUXER_OUTPUT_MPEG_4 ,则在输出视频中添加包含旋转角度的合成矩阵,以便视频播放器可以选择正确的播放方向。 请注意,某些视频播放器可能会在播放过程中选择忽略视频中的合成矩阵。 默认情况下,旋转度为0。
| 参数(Parameters) | |
|---|---|
degrees |
int: the angle to be rotated clockwise in degrees. The supported angles are 0, 90, 180, and 270 degrees. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if degree is not supported. |
IllegalStateException |
If this method is called after start(). |
void start ()
启动混合器。
确保在 addTrack(MediaFormat)之后和 writeSampleData(int, ByteBuffer, MediaCodec.BufferInfo)之前调用此 writeSampleData(int, ByteBuffer, MediaCodec.BufferInfo) 。
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
If this method is called after start() or Muxer is released |
void stop ()
停止混合器。
Once the muxer stops, it can not be restarted.
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if muxer is in the wrong state. |
void writeSampleData (int trackIndex,
ByteBuffer byteBuf,
MediaCodec.BufferInfo bufferInfo)
将编码的样本写入复用器。
应用程序需要确保样本被写入正确的轨道。 另外,它需要确保每个轨道的采样按时间顺序编写(例如,按编码器提供的顺序)。
| 参数(Parameters) | |
|---|---|
trackIndex |
int: The track index for this sample. |
byteBuf |
ByteBuffer: The encoded sample. |
bufferInfo |
MediaCodec.BufferInfo: The buffer information related to this sample. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if trackIndex, byteBuf or bufferInfo is invalid. |
IllegalStateException |
if muxer is in wrong state. MediaMuxer uses the flags provided in MediaCodec.BufferInfo, to signal sync frames. |
void finalize ()
当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 子类会覆盖finalize方法以处置系统资源或执行其他清理。
的常规协定finalize是,它被调用,如果当在Java TM虚拟机已确定不再有由该目的可以通过还没有死亡,除了作为一个动作的结果的任何线程访问的任何手段取决于某些其他可以完成的对象或类别的最终定稿。 方法finalize可以采取任何行动,包括使这个对象再次可用于其他线程; 然而, finalize的通常目的是在对象被不可撤销地丢弃之前执行清理操作。 例如,表示输入/输出连接的对象的finalize方法可能会执行显式I / O事务,以在永久丢弃该对象之前中断连接。
类Object的finalize方法Object执行特殊操作; 它只是正常返回。 Object子类可能会覆盖此定义。
Java编程语言不保证哪个线程将为任何给定对象调用finalize方法。 但是,保证调用finalize的线程在调用finalize时不会保留任何用户可见的同步锁。 如果finalize方法引发未捕获的异常,则忽略该异常,并终止该对象的终止。
在针对某个对象调用了 finalize方法之后,在Java虚拟机再次确定不再有任何方法可以通过尚未死亡的任何线程访问此对象,包括可能的操作通过准备完成的其他对象或类别,此时该对象可能被丢弃。
对于任何给定的对象,Java虚拟机从不会多次调用 finalize方法。
finalize方法引发的任何异常 finalize导致终止此对象的终止,但会被忽略。
| 抛出异常(Throws) | |
|---|---|
Throwable |
|