Implement screen capture without root on Android 5.0+ by using MediaProjectionManager, VirtualDisplay, AudioRecord, MediaCodec and MediaMuxer APIs
Branch: master
Clone or download
yrom BaseEncoder: prepare mediacodec on nonnull callback
See #21
MediaCodec maybe crash on some devices due to null callback
Latest commit 3db3992 May 24, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app BaseEncoder: prepare mediacodec on nonnull callback May 24, 2018
docs Add permissions explaining Dec 6, 2017
gradle/wrapper Make media codec and format parameters selectable Dec 3, 2017
.gitignore initial commit Nov 26, 2014
README.md Update release link Jan 9, 2018
build.gradle Update sdk versions Jan 9, 2018
gradle.properties screen recorder Nov 26, 2014
gradlew screen recorder Nov 26, 2014
gradlew.bat screen recorder Nov 26, 2014
screenshot.png Add screenshot and link to donwload apk Dec 5, 2017
settings.gradle

README.md

Screen Recorder

这是个DEMO APP 主要是实现了屏幕录制功能(可同时录制来自麦克风的声音)。

screenshot

Get it on Google Play 点此处下载APK 快速预览项目功能

说明:使用了 MediaProjectionManager, VirtualDisplay, AudioRecord, MediaCodec 以及 MediaMuxer 等API,故而这个项目最低支持Android 5.0。

录屏原理

** 注意 ** 你可以checkout 32c005412 查看原始的(不包含麦克风录制的)代码

  • Display 可以“投影”到一个 VirtualDisplay
  • 通过 MediaProjectionManager 取得的 MediaProjection创建VirtualDisplay
  • VirtualDisplay 会将图像渲染到 Surface中,而这个Surface是由MediaCodec所创建的
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
...
mSurface = mEncoder.createInputSurface();
...
mVirtualDisplay = mMediaProjection.createVirtualDisplay(name, mWidth, mHeight, mDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mSurface, null, null);
  • MediaMuxer 将从 MediaCodec 得到的图像元数据封装并输出到MP4文件中
int index = mEncoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_US);
...
ByteBuffer encodedData = mEncoder.getOutputBuffer(index);
...
mMuxer.writeSampleData(mVideoTrackIndex, encodedData, mBufferInfo);

所以其实在Android 4.4上可以通过DisplayManager来创建VirtualDisplay也是可以实现录屏,但因为权限限制需要ROOT。 (see DisplayManager.createVirtualDisplay())