Skip to content

Latest commit

 

History

History
44 lines (36 loc) · 2.4 KB

File metadata and controls

44 lines (36 loc) · 2.4 KB

Screen Recorder

This is a DEMO APP that mainly implements the screen recording function (it can also record the sound from the microphone).

screenshot

Get it on Google Play Click here to download APK Quick preview of project functions

Note: It uses APIs such as MediaProjectionManager, VirtualDisplay, AudioRecord, MediaCodec and MediaMuxer, so this project supports at least Android 5.0.

Screen Recording Principles

** Note ** You can checkout 32c005412 to view the original code (excluding microphone recording)

  • Display can be "projected" to a VirtualDisplay
  • Create a VirtualDisplay through a MediaProjection obtained from MediaProjectionManager
  • VirtualDisplay will render the image to a Surface, which is created by 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 encapsulates the image metadata obtained from MediaCodec and outputs it to the MP4 file
int index = mEncoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_US);
...
ByteBuffer encodedData = mEncoder.getOutputBuffer(index);
...
mMuxer.writeSampleData(mVideoTrackIndex, encodedData, mBufferInfo);

So in fact, on Android 4.4, you can create VirtualDisplay through DisplayManager and also record the screen, but because of permission restrictions, ROOT is required. (see DisplayManager.createVirtualDisplay())