Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding and encoding APIs are finally available. It was a thing since Android 4.1 in Google IO 2012, but until now, when a new Android version 4.2 has been released, those low-level APIs are still too hard to use. There're so many Android vendors, we can't even get the same results from all Google's Nexus devices.

Despite the annoyings, I still tried these APIs and prayed them to get better in next Android release, and next Android release would swallow market quikly.

I just finished a simple video player with the combination of MediaExtractor and MediaCodec, you may find the full project from my Github.

private MediaExtractor extractor;
private MediaCodec decoder;
private Surface surface; public void run() {
extractor = new MediaExtractor();
extractor.setDataSource(SAMPLE); for (int i = 0; i < extractor.getTrackCount(); i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
extractor.selectTrack(i);
decoder = MediaCodec.createDecoderByType(mime);
decoder.configure(format, surface, null, 0);
break;
}
} if (decoder == null) {
Log.e("DecodeActivity", "Can't find video info!");
return;
} decoder.start(); ByteBuffer[] inputBuffers = decoder.getInputBuffers();
ByteBuffer[] outputBuffers = decoder.getOutputBuffers();
BufferInfo info = new BufferInfo();
boolean isEOS = false;
long startMs = System.currentTimeMillis(); while (!Thread.interrupted()) {
if (!isEOS) {
int inIndex = decoder.dequeueInputBuffer(10000);
if (inIndex >= 0) {
ByteBuffer buffer = inputBuffers[inIndex];
int sampleSize = extractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
Log.d("DecodeActivity", "InputBuffer BUFFER_FLAG_END_OF_STREAM");
decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
isEOS = true;
} else {
decoder.queueInputBuffer(inIndex, 0, sampleSize, extractor.getSampleTime(), 0);
extractor.advance();
}
}
} int outIndex = decoder.dequeueOutputBuffer(info, 10000);
switch (outIndex) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
Log.d("DecodeActivity", "INFO_OUTPUT_BUFFERS_CHANGED");
outputBuffers = decoder.getOutputBuffers();
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
Log.d("DecodeActivity", "New format " + decoder.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
Log.d("DecodeActivity", "dequeueOutputBuffer timed out!");
break;
default:
ByteBuffer buffer = outputBuffers[outIndex];
Log.v("DecodeActivity"
, "We can't use this buffer but render it due to the API limit, " + buffer); // We use a very simple clock to keep the video FPS, or the video
// playback will be too fast
while (info.presentationTimeUs / 1000 > System.currentTimeMillis() - startMs) {
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
decoder.releaseOutputBuffer(outIndex, true);
break;
} if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
Log.d("DecodeActivity", "OutputBuffer BUFFER_FLAG_END_OF_STREAM");
break;
}
} decoder.stop();
decoder.release();
extractor.release();
}

andeoid硬件解码学习 (二)的更多相关文章

  1. android 硬件解码学习

    FileInputStream in = new FileInputStream("/sdcard/sample.ts"); String mimeType = "vid ...

  2. andeoid硬件解码

    Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...

  3. 集显也能硬件编码:Intel SDK && 各种音视频编解码学习详解

    http://blog.sina.com.cn/s/blog_4155bb1d0100soq9.html INTEL MEDIA SDK是INTEL推出的基于其内建显示核心的编解码技术,我们在播放高清 ...

  4. 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放 license收费等 ...

  5. 我的Android进阶之旅------>Android中编解码学习笔记

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  6. 【miscellaneous】各种音视频编解码学习详解

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  7. 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    目录(?)[-] 编解码学习笔记二codec类型 编解码学习笔记三Mpeg系列Mpeg 1和Mpeg 2 编解码学习笔记四Mpeg系列Mpeg 4 编解码学习笔记五Mpeg系列AAC音频 编解码学习笔 ...

  8. android Mediaplayer硬件解码浅探

    在讨论stagefright如何调用硬件解码之前,我们要先清楚几个问题. 我不展开这几个结论是如何得来的,因为这部分属于进程间通信binder的理解,和多媒体本身无关. 一.问题空间 这个有点像方法学 ...

  9. Python基础学习二

    Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...

随机推荐

  1. Firebase远程更新应用

    能打造出色的应用不意味着一定能在商业上取得成功,两者之间还有许多工作要做,绝不能简单发布应用后就宣告“收工”.您需要能迅速根据用户反馈作出调整.测试新功能,以及向用户提供他们最关注的内容. Fireb ...

  2. linux中萌翻了的cowsay命令

    想要让你的linux萌翻吗? 首先需要安装cowsay软件 命令如下:sudo apt-get update;sudo apt-get install cowsay 然后对其输入命令,默认情况下,动物 ...

  3. maven:安装m2eclipse插件

    一.安装maven 1.下载maven:http://pan.baidu.com/s/1hqIbx6s 2.解压并配置path路径(直接替换最新的maven包,即可升级) 3.测试 二.安装m2ecl ...

  4. UIScrollView的大全

    UIScrollView是iOS中提供滚动控件,用于解决当内容区域大于可视区域时不能完全查看的问题,UISrollView就可提供滑动方式查看全部内容,UISrollView是所有滚动视图的基类,后期 ...

  5. C++程序设计实践指导1.10二维数组元素换位改写要求实现

    改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数Double ...

  6. c中关于#与##的简易使用

    #运算符用于在预编译时,将宏参数转换为字符串 eg. #include <stdio.h>#define CONVERT(f)(#f) void helloworld(){ printf( ...

  7. 【solr专题之二】配置文件:solr.xml solrConfig.xml schema.xml

    1.关于默认搜索域 If you are using the Lucene query parser, queries that don't specify a field name will use ...

  8. 磁盘管理三-raid

    前言:何为raid raid是利用多个磁盘组成一个可提升效能.可包含冗余的磁盘阵列组.常用于数据吞吐量大(视频),冗余要求高的场景 当前raid包含了raid0-7,以及组合方式raid10,raid ...

  9. NAS4Free 配置BT下载

    NAS4Free 9.3.0.2 开启BT下载功能 Services|BitTorrent 选中右上角的复选框 Peer port 是监听端口,用于接受外部连接,需要在路由器配置该端口到服务器,才能提 ...

  10. How Many Tables(POJ 1213 求连通分量)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...