Screen recording implementation code of Android version 5.0 or above (complete code)

The way I record the screen is to record audio and video respectively, and finally merge them into MP4 format, which is more troublesome. Because there are few complete tutorials on the Internet, I intend to write a complete version. After writing my code, I can at least realize the function, rather than simply introduce the usage.

Since we are recording video, we should have a button to control the start and end.

2 before recording, you need to judge whether the version of Android system is greater than 5.0, and dynamically apply for permissions (read-write, recording, camera). This step can be executed when you click the start button

3 define some other necessary variables such as mediaprojection and mediaprojectionmanager

Write instantiation in oncreat

4 we handle the returned event in the onactivityresult callback method

5. First put the screenrecorder code. For friends who only want the results, paste the class directly and change the error reporting places (in my own project, there is no error reporting), so as to realize the function of recording the screen. If you still want to see, you can look down

6 start with the startrecorder method

Recording method

This is also the preparatory work

This is the beginning of writing video files

This is the method to combine the recorded audio and video into MP4. It is also used when clicking to stop recording the screen

This is the end time. I is used to judge whether the record has been recorded or not. It may be that you have just entered this page and have not recorded it. You may return to other pages directly, and there may be null pointer exceptions. Because some variables are not initialized, use I to judge. You can also write other methods to judge the end

I picked up part 7 of the code from the Internet, but I haven't seen a relatively complete version of the code on the Internet. What I wrote above has been tested by myself. There is absolutely no problem, and there is nothing missing in the code. If I find any missing code, I'll make it up later.

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
分享
二维码
< <上一篇
下一篇>>