如何录制Android应用Activity指定区域视频(无需Root与录屏权限)
Great question! The key here is to avoid system-level screen recording APIs (like MediaProjection) entirely—since those require permissions and show full-screen prompts. Instead, we'll capture your app's own UI directly (within your Activity) and record audio separately, then combine them into a video. This is exactly how apps like the one you referenced work.
Core Approach
We’ll break this into 3 straightforward steps:
- Capture frames from the 10dp-offset region of your Activity
- Record microphone audio
- Merge the frames and audio into a single video file
1. Capture the Target UI Region
Since we’re capturing your app’s own View hierarchy, we don’t need any special screen recording permissions. Here’s how to target the 10dp-padding region:
First, convert 10dp to pixel values (adjusts for device density):
int paddingDp = 10; float density = getResources().getDisplayMetrics().density; int paddingPx = (int) (paddingDp * density + 0.5f); // Round to nearest pixel
Then capture the relevant region of your Activity’s root View. We’ll use View.draw() for broad compatibility (works pre-API 24):
// Get the root view of your Activity ViewGroup rootView = findViewById(android.R.id.content); // Calculate the size of the region to capture int captureWidth = rootView.getWidth() - 2 * paddingPx; int captureHeight = rootView.getHeight() - 2 * paddingPx; // Create a bitmap to hold the captured frame Bitmap frameBitmap = Bitmap.createBitmap(captureWidth, captureHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(frameBitmap); // Offset the canvas to only draw the region 10dp inside the Activity bounds canvas.translate(-paddingPx, -paddingPx); rootView.draw(canvas); // Now frameBitmap contains the exact region you want to record
For smooth video, use a Handler to trigger captures at your desired frame rate (e.g., 25 frames per second). Reuse the same Bitmap object to avoid memory bloat.
2. Record Audio
To capture voice, you’ll need the RECORD_AUDIO permission (unavoidable for audio recording). Add it to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Request the permission dynamically (required for API 23+), then initialize the audio recorder:
MediaRecorder audioRecorder = new MediaRecorder(); File audioTempFile = new File(getExternalFilesDir(null), "temp_audio.mp4"); try { audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); audioRecorder.setOutputFile(audioTempFile.getAbsolutePath()); audioRecorder.prepare(); audioRecorder.start(); } catch (IOException e) { e.printStackTrace(); }
3. Combine Frames and Audio into Video
Use MediaCodec (to encode frames into a video stream) and MediaMuxer (to merge video and audio tracks) to create the final MP4 file.
Quick Overview of the Process:
- Configure
MediaCodecwith your capture resolution, frame rate, and H.264 codec (video/avc). - Feed each captured
Bitmapinto the codec as a video frame. - Once recording stops, stop the audio recorder and use
MediaMuxerto write both the video and audio tracks to a single output file.
If you want to simplify this step, lightweight open-source libraries can handle encoding/muxing without heavy dependencies like FFmpeg.
Why This Works
- No Root/Screen Record Permissions: We’re only capturing your app’s own UI, not the entire system screen. This avoids the system’s screen recording prompt and root requirements.
- Targeted Region: The canvas offset ensures we precisely record the area 10dp inside your Activity’s bounds.
- Audio Included: Independent audio capture via
MediaRecorderlets us merge voice with the video seamlessly.
Important Considerations
- Performance: Reuse
Bitmapobjects and avoid unnecessary object creation to prevent memory leaks. - Frame Rate: Stick to 20-30 FPS for smooth, standard-quality video.
- Permission Handling: Always check and request
RECORD_AUDIOpermission at runtime for Android 6.0+.
内容的提问来源于stack exchange,提问作者Ashutosh Kumar

