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();
}

最新文章

  1. 求余VS求模--C语言中表述
  2. linux python pip包安装
  3. csharp: Sound recording
  4. poj 2007 Scrambled Polygon(极角排序)
  5. maven学习手记 - 1
  6. POJ1046Color Me Less
  7. tomcat配置多个web网站的配置详解
  8. C嵌入汇编
  9. .net下二进制序列化的格式分析[转]
  10. nodejs爬虫笔记(二)---代理设置
  11. python自动化开发-[第九天]-异常处理、进程
  12. 细说java系列之泛型
  13. 为Oracle GoldenGate准备数据库
  14. easyui的datagrid和treegrid的使用
  15. tomcat gzip
  16. 【大数据系列】win10上安装hadoop开发环境
  17. Android逆向-Android基础逆向(5)
  18. [WinCE | VS2008 | Solution] VS2008 building WinCE projects taking a long time
  19. mysql5.7新特性探究
  20. 4_python之路之模拟工资管理系统

热门文章

  1. 通过 jsp+ajax+servlet+webservice 远程访问天气预报服务
  2. Mac air苹果笔记本安装Win10双系统教程(绝对能成功,超详细!)[转]
  3. shell特殊符号用法大全
  4. NYOJ 27.水池数目-DFS求连通块
  5. Mysql varchar长度问题
  6. CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)
  7. bzoj 5092: [Lydsy1711月赛]分割序列
  8. Ubuntu下sudo apt-get install vim 失败的解决办法
  9. EasyMvc入门教程-高级控件说明(15)方位布局控件
  10. ylb:日期和时间函数