Skip to content

This is a simple application to record all kinds of phone screens based on Android platform.

Notifications You must be signed in to change notification settings

Ezharjan/PhoneScreenRecorder

Repository files navigation

Phone Screen Recorder

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

screenshot

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

录屏原理

  • 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())

About

This is a simple application to record all kinds of phone screens based on Android platform.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages