android 原生 MediaPlayer 和 MediaCodec 的区别和联系(三)


- native raw video format:这由 MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface 标记,并且它可以和输入输出缓冲区一起使用。
- flexible YUV buffers(例如MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible):通过使用getInput/OutputImage(int) ,它们可以与输入/输出 Surface 一起使用,也可以在 ByteBuffer 模式下使用。
- other, specific formats:这些通常仅在 ByteBuffer 模式下受支持。某些颜色格式是供应商特定的。 其他在MediaCodecInfo.CodecCapabilities中定义。 对于等效于灵活格式的颜色格式,您仍然可以使用getInput/OutputImage(int)。

MediaFormat format = decoder.getOutputFormat(…);
int width = format.getInteger(MediaFormat.KEY_WIDTH);
if (format.containsKey("crop-left") && format.containsKey("crop-right")) {
width = format.getInteger("crop-right") + 1 - format.getInteger("crop-left");
}
int height = format.getInteger(MediaFormat.KEY_HEIGHT);
if (format.containsKey("crop-top") && format.containsKey("crop-bottom")) {
height = format.getInteger("crop-bottom") + 1 - format.getInteger("crop-top");
}








MediaCodec codec = MediaCodec.createByCodecName(name);
MediaFormat mOutputFormat; // member variable
codec.setCallback(new MediaCodec.Callback() {
@Override
void onInputBufferAvailable(MediaCodec mc, int inputBufferId) {
ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId);
// fill inputBuffer with valid data
…
codec.queueInputBuffer(inputBufferId, …);
} @Override
void onOutputBufferAvailable(MediaCodec mc, int outputBufferId, …) {
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);
MediaFormat bufferFormat = codec.getOutputFormat(outputBufferId); // option A
// bufferFormat is equivalent to mOutputFormat
// outputBuffer is ready to be processed or rendered.
…
codec.releaseOutputBuffer(outputBufferId, …);
} @Override
void onOutputFormatChanged(MediaCodec mc, MediaFormat format) {
// Subsequent data will conform to new format.
// Can ignore if using getOutputFormat(outputBufferId)
mOutputFormat = format; // option B
} @Override
void onError(…) {
…
}
});
codec.configure(format, …);
mOutputFormat = codec.getOutputFormat(); // option B
codec.start();
// wait for processing to complete
codec.stop();
codec.release();
MediaCodec codec = MediaCodec.createByCodecName(name);
codec.configure(format, …);
MediaFormat outputFormat = codec.getOutputFormat(); // option B
codec.start();
for (;;) {
int inputBufferId = codec.dequeueInputBuffer(timeoutUs);
if (inputBufferId >= 0) {
ByteBuffer inputBuffer = codec.getInputBuffer(…);
// fill inputBuffer with valid data
…
codec.queueInputBuffer(inputBufferId, …);
}
int outputBufferId = codec.dequeueOutputBuffer(…);
if (outputBufferId >= 0) {
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);
MediaFormat bufferFormat = codec.getOutputFormat(outputBufferId); // option A
// bufferFormat is identical to outputFormat
// outputBuffer is ready to be processed or rendered.
…
codec.releaseOutputBuffer(outputBufferId, …);
} else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// Subsequent data will conform to new format.
// Can ignore if using getOutputFormat(outputBufferId)
outputFormat = codec.getOutputFormat(); // option B
}
}
codec.stop();
codec.release();
MediaCodec codec = MediaCodec.createByCodecName(name);
codec.configure(format, …);
codec.start();
ByteBuffer[] inputBuffers = codec.getInputBuffers();
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
for (;;) {
int inputBufferId = codec.dequeueInputBuffer(…);
if (inputBufferId >= 0) {
// fill inputBuffers[inputBufferId] with valid data
…
codec.queueInputBuffer(inputBufferId, …);
}
int outputBufferId = codec.dequeueOutputBuffer(…);
if (outputBufferId >= 0) {
// outputBuffers[outputBufferId] is ready to be processed or rendered.
…
codec.releaseOutputBuffer(outputBufferId, …);
} else if (outputBufferId == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = codec.getOutputBuffers();
} else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// Subsequent data will conform to new format.
MediaFormat format = codec.getOutputFormat();
}
}
codec.stop();
codec.release();
流末端处理(End-of-stream Handling)
- 不渲染缓冲区:调用releaseOutputBuffer(bufferId, false)。
- 使用默认时间戳呈现缓冲区:调用releaseOutputBuffer(bufferId, true)。
- 使用特定时间戳呈现缓冲区:调用releaseOutputBuffer(bufferId, timestamp)。

- 可恢复的错误:如果isRecoverable()返回 true,则调用stop(),configure(…)和start()进行恢复。
- 瞬态错误:如果iisTransient()返回 true,则资源暂时不可用,并且可以在以后重试该方法。
- 致命错误:如果isRecoverable()和isTransient()都返回 false,则CodecException是致命的,并且必须重置(reset )或释放编解码器(released)。
android 原生 MediaPlayer 和 MediaCodec 的区别和联系(三)的更多相关文章
- Android 原生 MediaPlayer 和 MediaCodec 的区别和联系(二)
目录: (3)Android 官方网站 对 MediaPlayer的介绍 正文: Android 官方网站 对 MediaPlayer的介绍 MediaPlayer pub ...
- Android MediaPlayer 和 MediaCodec 的区别和联系(一)
目录: (1)概念解释 : 硬解.软解 (2)Intel关于Android MediaCodec的相关说明 正文: 一.硬解.软解 (1)概念: a.硬 ...
- Android原生编解码接口 MediaCodec 之——踩坑
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/gb702250823/article/d ...
- Android中style和theme的区别
在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...
- Android进阶(二十七)Android原生扰人烦的布局
Android原生扰人烦的布局 在开发Android应用时,UI布局是一件令人烦恼的事情.下面主要讲解一下Android中的界面布局. 一.线性布局(LinearLayout) 线性布局分为: (1) ...
- 像写Flutter一样开发Android原生应用
要问到Flutter和Android原生App,在开发是有何区别,编程方式是绕不开的话题.Flutter采用声明式编程,Android原生开发则采用命令式编程. 声明式编程 VS. 命令式编程 我们首 ...
- 拓展 Android 原生 CountDownTimer 倒计时
拓展 Android 原生 CountDownTimer 倒计时 [TOC] CountDownTimer 在系统的CountDownTimer上进行的修改,主要是拓展了功能,当然也保留了系统默认的模 ...
- Android原生json和fastjson的简单使用
android原生操作json数据 主要是两个类 JSONObject 操作对象 JONSArray操作json数组 对象转json //创建学生对象 Student student=new ...
- Android原生游戏开发:使用JustWeEngine开发微信打飞机
使用JustWeEngine开发微信打飞机: 作者博客: 博客园 引擎地址:JustWeEngine 示例代码:EngineDemo JustWeEngine? JustWeEngine是托管在Git ...
随机推荐
- Cisco ISR4400 Netflow 配置模板
flow exporter NAME destination 145.0.1.200 transport udp 9991 export-protocol netflow-v5 flow monito ...
- Springboot用官方建议访问Html页面并接传值
Springboot用官方建议访问Html页面并接传值 我们以前通常习惯用webapp来防止jsp页面,但是到了Springboot中,官方建议用Static文件夹来存放及静态的资源, 用templa ...
- 关于JNI调用从eclipse转到Android Studio遇到的问题(总结)
将一个小应用从eclipse开发迁移到android studio,程序中有native代码实现,在eclipse是靠Android.mk这么个mk文件来组织编译的,但到android studio上 ...
- 群晖MyDS账号注册--实现使用QuickConnect外网访问
最近公司拿了个NAS给我,让我把它配置好,之前没有接触过这个东西,上网一查,发现就是和去年很火的玩客云和斐讯天天链N1的功能一样,可以实现文件储存和文件共享. 设备型号:群晖DS214SE 系统版本: ...
- C#(winform)设置窗口置顶
只要设置窗体的TopMost属性即可: registerForm.TopMost = true;
- 常用工具说明--mongodb、mysql解压版、IDEA配置maven
Mongodb的安装.配置 1.去官网下载mongodb安装包,mongodb官网.点击右上角的 Download,下载对应的msi安装包 2.安装程序,选择 Custom,自定义安装路径,比如安装在 ...
- nvm安装最新稳定版node
安装当前最新的稳定版. nvm install stable
- Mongodb操作之查询(循序渐进对比SQL语句)(转http://www.tuicool.com/articles/UzQj6rF)
工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活. 集合简单查询方法 mongodb语法:db.collection.find() //co ...
- 6、Object、String、StringBuffer
Java的Api以及Object类 API概念 * A:API(Application Programming Interface) * 应用程序编程接口 * B:Java API * 就是Java提 ...
- java的内存区域
java的内存区域分为程序计数器.java虚拟机栈.本地方法栈.java堆.方法区.运行时常量池. 1.程序计数器 2.java虚拟机栈 3.本地方法栈 4.java堆(新生代和老年代) 5.方法区( ...