Implementation of Android screen recording function

Recently, an android development project used the screen recording function. I began to consult some materials and blogs, basically discussing root. It was not until I saw a relatively new code on GitHub that I suddenly found that Android 5.0 opened a new interface - android.media.projection, which suddenly made this problem simple. Therefore, we should also pay attention to the real-time performance of data access. Now many technologies push through the old and bring forth the new quickly. Some new packages, interfaces and methods will make the problems better and faster solved. However, I still decided to summarize some previous ideas, which can be regarded as a learning.

First, let's talk about the previous methods. Generally, the first Android screen capture program comes from the screen capture method of Linux. Android uses the Linux kernel, so the screen capture method under Linux is first adopted by Android. Linux uses framebuffer to manage display output. The traditional method is to read the data of framebuffer and get the data of the whole screen. This method was also the only feasible method before Android 3.0. However, Linux adopts strict permission control over device files, and framebuffer is also one of its controls. In Android, only root and graphic users have permission to read. Therefore, in order to read the screenshot by reading framebuffer, the application must obtain root permission.

Later, we all know that root permission is a troublesome thing, and adding root permission to our own software is also a very risky thing for customers. Therefore, a commonly used method later is to customize the view and obtain the current screen in the form of bitmap. The core code is:

Then save the bitmap. However, there is an obvious defect in this method, that is, the cut-out diagram cannot obtain the status bar and some dialogs.

In addition, the more common methods include reading framebuffer, using interface surfaceflinger, screen capture through DDMS and other methods, but they all have limitations.

So Google released the current interface android.media.project.mediaproject in Android 5.0. Using this interface, third-party applications can directly take screenshots without obtaining system root permission. By querying its official API, it can be seen that this interface is mainly used for "screen capture" operation and "audio recording" operation. Well, go straight to the dry goods.

1、 Method of use.

First, use the parameter media_- PROJECTION_ Service calls context. Getsystemservice() to get the mediaprojectionmanager class instance;

Secondly, call createscreencaptureintent() to get an intent; Again, use startactivityforresult() to start screen capture;

Finally, return the result to getmediaprojection () to get the captured data.

2、 Demo case.

1. Import interface

2. Instantiate mediaprojectionmanager

3. Use the function function createscreencaptureintent() of the mediaprojectionmanager class instance to generate an intent to prepare for the next screen capture

4. Get the return value in onactivityresult()

5. Instantiate virtualdisplay. The main function of this class is to obtain screen information and save it in the.

6. Save the picture.

The current picture information is kept in the msurface. Create an image, save the obtained information in the bufferbyte, and then save it in a bitmap.

In this way, the whole screen capture becomes much simpler. In addition, mediacodec coding and mediamuxer encapsulation can be used to record the screen in MP4 format, which is simple and easy to use

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>