简体   繁体   English

Android MediaCodec API视频播放速度过快

[英]Android MediaCodec API video plays too fast

I'm currently working with Android Jelly Bean MediaCodec API to develop a simple video player. 我目前正在使用Android Jelly Bean MediaCodec API来开发一个简单的视频播放器。

I extract tracks, play audio and video in separate threads. 我在单独的线程中提取曲目,播放音频和视频。 The problem is that video track always is played too fast. 问题是视频播放总是播放太快。

Where can be the problem hidden? 哪里可以隐藏问题?
Both audio and video are treated almost the same way, except audio is played via AudioTrack and video is rendered to the surface. 音频和视频的处理方式几乎相同,但音频通过AudioTrack播放,视频呈现在表面。

If you render frames at maximum speed you'll hit 60fps on most devices. 如果以最大速度渲染帧,则在大多数设备上都会达到60fps。 You need to pace it according to the presentation time stamps provided by the encoder. 您需要根据编码器提供的演示时间标记来调整速度。

For example, if the input is a format supported by Android (eg a typical .mp4 file), you can use the MediaExtractor class to extract each frame. 例如,如果输入是Android支持的格式(例如典型的.mp4文件),则可以使用MediaExtractor类提取每个帧。 The time stamp can be retrieved with getSampleTime() . 可以使用getSampleTime()检索时间戳。 You want to delay rendering by the difference between timestamps on consecutive frames -- don't assume that the first frame will have a timestamp of zero. 您希望通过连续帧上的时间戳之间的差异来延迟渲染 - 不要假设第一帧的时间戳为零。

Also, don't assume that video frames appear at a constant rate (eg 30fps). 此外,不要假设视频帧以恒定速率出现(例如30fps)。 For some sources the frames will arrive unevenly. 对于某些来源,帧将不均匀地到达。

See the "Play video (TextureView)" example in Grafika , particularly the SpeedControlCallback class. 请参阅“播放视频(TextureView)”中例如Grafika ,特别是SpeedControlCallback类。 The gen-eight-rects.mp4 video uses variable frame durations to exercise it. gen-eight-rects.mp4视频使用可变帧持续时间来练习它。 If you check the "Play at 60fps" box, the presentation time stamps are ignored. 如果选中“以60fps播放”框,则会忽略演示时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM