Most visited

Recently visited

Added in API level 23

MediaSync

public final class MediaSync
extends Object

java.lang.Object
   ↳ android.media.MediaSync


MediaSync类可用于同步播放音频和视频流。 它也可以用来播放纯音频或纯视频流。

MediaSync通常是这样使用的:

 MediaSync sync = new MediaSync();
 sync.setSurface(surface);
 Surface inputSurface = sync.createInputSurface();
 ...
 // MediaCodec videoDecoder = ...;
 videoDecoder.configure(format, inputSurface, ...);
 ...
 sync.setAudioTrack(audioTrack);
 sync.setCallback(new MediaSync.Callback() {
     @Override
     public void onAudioBufferConsumed(MediaSync sync, ByteBuffer audioBuffer, int bufferId) {
         ...
     }
 }, null);
 // This needs to be done since sync is paused on creation.
 sync.setPlaybackParams(new PlaybackParams().setSpeed(1.f));

 for (;;) {
   ...
   // send video frames to surface for rendering, e.g., call
   // videoDecoder.releaseOutputBuffer(videoOutputBufferIx, videoPresentationTimeNs);
   // More details are available as below.
   ...
   sync.queueAudio(audioByteBuffer, bufferId, audioPresentationTimeUs); // non-blocking.
   // The audioByteBuffer and bufferId will be returned via callback.
   // More details are available as below.
   ...
     ...
 }
 sync.setPlaybackParams(new PlaybackParams().setSpeed(0.f));
 sync.release();
 sync = null;

 // The following code snippet illustrates how video/audio raw frames are created by
 // MediaCodec's, how they are fed to MediaSync and how they are returned by MediaSync.
 // This is the callback from MediaCodec.
 onOutputBufferAvailable(MediaCodec codec, int bufferId, BufferInfo info) {
     // ...
     if (codec == videoDecoder) {
         // surface timestamp must contain media presentation time in nanoseconds.
         codec.releaseOutputBuffer(bufferId, 1000 * info.presentationTime);
     } else {
         ByteBuffer audioByteBuffer = codec.getOutputBuffer(bufferId);
         sync.queueAudio(audioByteBuffer, bufferId, info.presentationTime);
     }
     // ...
 }

 // This is the callback from MediaSync.
 onAudioBufferConsumed(MediaSync sync, ByteBuffer buffer, int bufferId) {
     // ...
     audioDecoder.releaseBuffer(bufferId, false);
     // ...
 }

 
The client needs to configure corresponding sink by setting the Surface and/or AudioTrack based on the stream type it will play.

对于视频来说,客户端需要调用 createInputSurface()来获取它将渲染视频帧的表面。

对于音频,客户端需要正确设置音轨,例如使用MODE_STREAM 音频缓冲区直接通过queueAudio(ByteBuffer, int, long)发送到MediaSync,并通过异步方式onAudioBufferConsumed(MediaSync, ByteBuffer, int)返回给客户端。 客户端在返回之前不应该修改音频缓冲区。

客户端可以通过将播放速率设置为0.0来预先填充音频/视频缓冲区,然后将音频/视频缓冲区提供给相应的组件。 这可以减少可能的初始欠载。

Summary

Nested classes

class MediaSync.Callback

MediaSync回调界面。

interface MediaSync.OnErrorListener

在异步操作期间发生错误时要调用的回调的接口定义(其他错误将在方法调用时引发异常)。

Constants

int MEDIASYNC_ERROR_AUDIOTRACK_FAIL

音轨失败。

int MEDIASYNC_ERROR_SURFACE_FAIL

表面无法处理视频缓冲区。

Public constructors

MediaSync()

类构造函数。

Public methods

final Surface createInputSurface()

请求Surface用作输入。

void flush()

刷新同步对象中的所有缓冲区。

PlaybackParams getPlaybackParams()

使用 PlaybackParams获取回放速率。

SyncParams getSyncParams()

获取A / V同步模式。

MediaTimestamp getTimestamp()

获取当前播放位置。

void queueAudio(ByteBuffer audioData, int bufferId, long presentationTimeUs)

异步排队音频数据进行播放(AudioTrack必须处于流模式)。

final void release()

确保在你完成释放任何打开的组件实例时调用它,而不是依赖垃圾回收器在将来的某个时刻为你做这件事。

void setAudioTrack(AudioTrack audioTrack)

设置MediaSync的音轨。

void setCallback(MediaSync.Callback cb, Handler handler)

为可操作的MediaSync事件设置异步回调。

void setOnErrorListener(MediaSync.OnErrorListener listener, Handler handler)

为错误事件设置异步回调。

void setPlaybackParams(PlaybackParams params)

使用 PlaybackParams设置播放速率。

void setSurface(Surface surface)

设置MediaSync的输出表面。

void setSyncParams(SyncParams params)

设置A / V同步模式。

Protected methods

void finalize()

当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。

Inherited methods

From class java.lang.Object

Constants

MEDIASYNC_ERROR_AUDIOTRACK_FAIL

Added in API level 23
int MEDIASYNC_ERROR_AUDIOTRACK_FAIL

音轨失败。

也可以看看:

常数值:1(0x00000001)

MEDIASYNC_ERROR_SURFACE_FAIL

Added in API level 23
int MEDIASYNC_ERROR_SURFACE_FAIL

表面无法处理视频缓冲区。

也可以看看:

常量值:2(0x00000002)

Public constructors

MediaSync

Added in API level 23
MediaSync ()

类构造函数。 创建时,MediaSync暂停,即播放速率为0.0f。

Public methods

createInputSurface

Added in API level 23
Surface createInputSurface ()

请求Surface用作输入。 这只能在setSurface(Surface)之后setSurface(Surface)

应用程序负责在完成时调用Surface上的release()。

Returns
Surface
Throws
IllegalStateException if not set, or another input surface has already been created.

flush

Added in API level 23
void flush ()

刷新同步对象中的所有缓冲区。

所有待处理的音频和视频缓冲区均被丢弃。 如果配置了音轨,则会刷新并停止。 如果配置了视频输出界面,排队的最后一帧将留在帧上。 排队一个空白的视频帧来清除表面,

没有收到刷新的缓冲区的回调。

Throws
IllegalStateException if the internal player engine has not been initialized.

getPlaybackParams

Added in API level 23
PlaybackParams getPlaybackParams ()

使用 PlaybackParams获取回放速率。

Returns
PlaybackParams the playback rate being used.
Throws
IllegalStateException if the internal sync engine or the audio track has not been initialized.

getSyncParams

Added in API level 23
SyncParams getSyncParams ()

获取A / V同步模式。

Returns
SyncParams the A/V sync params
Throws
IllegalStateException if the internal player engine has not been initialized.

getTimestamp

Added in API level 23
MediaTimestamp getTimestamp ()

获取当前播放位置。

MediaTimestamp表示媒体时间如何使用锚点和时钟频率以线性方式与系统时间相关联。 在正常播放过程中,媒体时间会相当持续地移动(尽管锚帧可能会重新设置为当前系统时间,但线性相关保持稳定)。 因此,这种方法不需要经常调用。

为了帮助用户获得当前播放位置,此方法始终将时间戳固定到当前的 system time ,因此 getAnchorMediaTimeUs()可用作当前播放位置。

Returns
MediaTimestamp a MediaTimestamp object if a timestamp is available, or null if no timestamp is available, e.g. because the media player has not been initialized.

也可以看看:

queueAudio

Added in API level 23
void queueAudio (ByteBuffer audioData, 
                int bufferId, 
                long presentationTimeUs)

异步排队音频数据进行播放(AudioTrack必须处于流模式)。 如果由于flush()而导致音轨被刷新,它将被重新启动。

Parameters
audioData ByteBuffer: the buffer that holds the data to play. This buffer will be returned to the client via registered callback.
bufferId int: an integer used to identify audioData. It will be returned to the client along with audioData. This helps applications to keep track of audioData, e.g., it can be used to store the output buffer index used by the audio codec.
presentationTimeUs long: the presentation timestamp in microseconds for the first frame in the buffer.
Throws
IllegalStateException if audio track is not set or internal configureation has not been done correctly.

release

Added in API level 23
void release ()

确保在你完成释放任何打开的组件实例时调用它,而不是依赖垃圾回收器在将来的某个时刻为你做这件事。

setAudioTrack

Added in API level 23
void setAudioTrack (AudioTrack audioTrack)

设置MediaSync的音轨。

目前,这仅在初始化状态下受支持。

Parameters
audioTrack AudioTrack: Specify an AudioTrack through which to render the audio data.
Throws
IllegalArgumentException if the audioTrack has been released, or is invalid.
IllegalStateException if setting the audio track is not supported, e.g. not in the Initialized state, or another audio track has already been set.

setCallback

Added in API level 23
void setCallback (MediaSync.Callback cb, 
                Handler handler)

为可操作的MediaSync事件设置异步回调。

可以多次调用此方法以更新先前设置的回调。 如果处理程序发生更改,则可能会丢弃为旧处理程序计划的未送达通知。

不要在回调中调用这个。

Parameters
cb MediaSync.Callback: The callback that will run. Use null to stop receiving callbacks.
handler Handler: The Handler that will run the callback. Use null to use MediaSync's internal handler if it exists.

setOnErrorListener

Added in API level 23
void setOnErrorListener (MediaSync.OnErrorListener listener, 
                Handler handler)

为错误事件设置异步回调。

可以多次调用此方法来更新以前设置的侦听器。 如果处理程序发生更改,则可能会丢弃为旧处理程序计划的未送达通知。

不要在回调中调用这个。

Parameters
listener MediaSync.OnErrorListener: The callback that will run. Use null to stop receiving callbacks.
handler Handler: The Handler that will run the callback. Use null to use MediaSync's internal handler if it exists.

setPlaybackParams

Added in API level 23
void setPlaybackParams (PlaybackParams params)

使用 PlaybackParams设置播放速率。

将MediaSync与 AudioTrack一起使用 AudioTrack ,使用此调用设置回放参数,而不是直接在轨道上调用它,以便同步知道参数更改。

如果没有音轨,此调用也可以使用。

Parameters
params PlaybackParams: the playback params to use. Speed is the ratio between desired playback rate and normal one. 1.0 means normal playback speed. 0.0 means pause. Value larger than 1.0 means faster playback, while value between 0.0 and 1.0 for slower playback. Note: the normal rate does not change as a result of this call. To restore the original rate at any time, use speed of 1.0.
Throws
IllegalStateException if the internal sync engine or the audio track has not been initialized.
IllegalArgumentException if the params are not supported.

setSurface

Added in API level 23
void setSurface (Surface surface)

设置MediaSync的输出表面。

目前,这仅在初始化状态下受支持。

Parameters
surface Surface: Specify a surface on which to render the video data.
Throws
IllegalArgumentException if the surface has been released, is invalid, or can not be connected.
IllegalStateException if setting the surface is not supported, e.g. not in the Initialized state, or another surface has already been set.

setSyncParams

Added in API level 23
void setSyncParams (SyncParams params)

设置A / V同步模式。

Parameters
params SyncParams: the A/V sync params to apply
Throws
IllegalStateException if the internal player engine has not been initialized.
IllegalArgumentException if params are not supported.

Protected methods

finalize

Added in API level 23
void finalize ()

当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 子类重写finalize方法以处置系统资源或执行其他清理。

finalize的一般合同是,如果当Java TM虚拟机已经确定不再有任何方法可以通过任何尚未死亡的线程访问该对象时,除非作为动作的结果取决于某些其他可以完成的对象或类别的最终定稿。 方法finalize可以采取任何行动,包括使该对象再次可用于其他线程; 然而, finalize的通常目的是在对象被不可撤销地丢弃之前执行清理操作。 例如,表示输入/输出连接的对象的finalize方法可能会执行显式I / O事务,以在永久丢弃该对象之前中断连接。

finalize方法Object执行特殊的操作; 它只是正常返回。 Object子类可能会覆盖此定义。

Java编程语言不保证哪个线程将为任何给定对象调用finalize方法。 但是,保证调用finalize的线程在调用finalize时不会保留任何用户可见的同步锁。 如果finalize方法引发未捕获的异常,则忽略该异常,并终止该对象的终止。

在针对对象调用 finalize方法之后,不会采取进一步的操作,直到Java虚拟机再次确定不再有任何尚未死亡的线程可以访问此对象的方式,包括可能的操作通过准备完成的其他对象或类别,此时该对象可能被丢弃。

对于任何给定的对象,Java虚拟机从不会多次调用 finalize方法。

finalize方法抛出的任何异常 finalize导致终止此对象的终止,但会被忽略。

Hooray!