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

Note: It uses APIs such as MediaProjectionManager, VirtualDisplay, AudioRecord, MediaCodec and MediaMuxer, so this project supports at least Android 5.0.
** Note ** You can checkout 32c005412 to view the original code (excluding microphone recording)
Displaycan be "projected" to aVirtualDisplay- Create a
VirtualDisplaythrough aMediaProjectionobtained fromMediaProjectionManager VirtualDisplaywill render the image to aSurface, which is created byMediaCodec
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
...
mSurface = mEncoder.createInputSurface();
...
mVirtualDisplay = mMediaProjection.createVirtualDisplay(name, mWidth, mHeight, mDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mSurface, null, null);
MediaMuxerencapsulates the image metadata obtained fromMediaCodecand 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())
