Capture and Record Android Screen using MediaProjection APIs

Android MediaProjection Featured

One of the new APIs introduced in Android 5.0 is the MediaProjection API. According to official documentation this API gives us the ability to capture screen contents with audio recording. As you may know screen cast is one of the new features introduced in Android. This Android MediaProjection API is the core API through which the screen cast takes place. By using this Android MediaProjection API we can also share screen over the network. This API also solves a very basic function which was needed in Android i.e. recording of screen programmatically. But sadly there are no samples available on how to do this. Therefore in this Capture and Record Android Screen using MediaProjection APIs tutorial, I would make an example where a video of device screen would be recorded, with audio.

Although Android media projection API solves the problem of recording and capturing device’s screen, but since it was introduced in lollipop version of android therefore as of now it cannot be used on API version lower than 21.

Android Media Projection Example : Record Screen Video by Code

To record our screen activity using Android media projection class we will be using MediaRecorder class, which would actually record the screen activity. Android media recorder class is a simple class used for recording any type of audio and video. In this example we will be using the MediaRecorder class to capture the output of MediaProjection class once the capture intent is fired. But before doing so please add these permissions in your manifest.

Please Note: If you don’t wish to record audio, then you don’t need to add the record audio permission. Only write external storage would be required.

Next, to use Android MediaProjection API lets make a layout:

Please note: truiton_short is just a drawable image, in your code, you may choose exclude it.

The above file would give a layout like this:

Android Record Screen using MediaProjection


Next lets have a look at the main activity where screen recording would take place using the Android media projection APIs.

As you may see above, to start a media projection first an intent is created using createScreenCaptureIntent() method of MediaProjectionManager class. When user approves the screen cast request, media projection is started from the onActivityResult method, along with the media recording. By doing this, we are allowing the app to start a screen cast which is recorded on the device itself. This would generate an audio/video file in mp4 format on your SD card. Although if you don’t wish to record audio, you can can remove the setAudioSource and setAudioEncoder lines from the initRecorder() method. To view the full source code please visit the repo below:

Full Source


The place where both Android MediaProjection class and MediaRecording class interact is when creating a virtual display using the createVirtualDisplay method. This method basically creates a virtual display where the screen contents are buffered on to a Surface. To capture the screen contents in a MediaRecorder, I initialized a media recorder above with video source as a surface using the MediaRecorder.VideoSource.SURFACE parameter. Then in the createVirtualDisplay method I specified the surface of media recorder by using mMediaRecorder.getSurface() method. By doing this I gave the input to media recorder from the media projection class in real time, through which a screen capture video recording file is generated. Hope this helps. Connect with us on Facebook, Twitter, and Google+.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *

48 thoughts on “Capture and Record Android Screen using MediaProjection APIs