需求如下图 实现系统录屏功能
frameworks/base/packages/SystemUI/src/com/android/systemui/screenrecord
涉及代码
ScreenRecordDialog # startBtn
RecordingService # startRecording# stopRecording
ScreenMediaRecorder # start # end #save
1、点击开始录屏frameworks/base/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenRecordDialog.java
startBtn.setOnClickListener(v -> {
if (mOnStartRecordingClicked != null) {
// Note that it is important to run this callback before dismissing, so that the
// callback can disable the dialog exit animation if it wants to.
mOnStartRecordingClicked.run();
}
requestScreenCapture();
dismiss();
});
2、ScreenRecordDialog #requestScreenCapture核心代码实现功能
private void requestScreenCapture() {
Context userContext = mUserContextProvider.getUserContext();
ScreenRecordingAudioSource audioMode = mAudioSwitch.isChecked()
? (ScreenRecordingAudioSource) mOptions.getSelectedItem()
: NONE;
PendingIntent startIntent = PendingIntent.getForegroundService(userContext,
RecordingService.REQUEST_CODE,
RecordingService.getStartIntent(
userContext, Activity.RESULT_OK,
audioMode.ordinal()),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
PendingIntent stopIntent = PendingIntent.getService(userContext,
RecordingService.REQUEST_CODE,
RecordingService.getStopIntent(userContext),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
mController.startCountdown(DELAY_MS, INTERVAL_MS, startIntent, stopIntent);
}
}
3、RecordingService 中实现开始录屏功能 代码如下图所示
case ACTION_START:
mAudioSource = ScreenRecordingAudioSource
.values()[intent.getIntExtra(EXTRA_AUDIO_SOURCE, 0)];
Log.d(TAG, "recording with audio source" + mAudioSource);
mRecorder = new ScreenMediaRecorder(
mUserContextTracker.getUserContext(),
currentUserId,
mAudioSource,
this
);
if (startRecording()) {
updateState(true);
createRecordingNotification();
mUiEventLogger.log(Events.ScreenRecordEvent.SCREEN_RECORD_START);
} else {
updateState(false);
createErrorNotification();
stopForeground(true);
stopSelf();
return Service.START_NOT_STICKY;
}
break;
4、ScreenMediaRecorder 调用start方法开始录屏
* Start screen recording
*/
void start() throws IOException, RemoteException, RuntimeException {
Log.d(TAG, "start recording");
prepare();
mMediaRecorder.start();
recordInternalAudio();
}
5、ScreenMediaRecorder停止录屏 调用代码路径
/**
* End screen recording
*/
void end() {
mMediaRecorder.stop();
mMediaRecorder.release();
mInputSurface.release();
mVirtualDisplay.release();
mMediaProjection.stop();
mMediaRecorder = null;
mMediaProjection = null;
stopInternalAudioRecording();
Log.d(TAG, "end recording");
}
6、保存录屏视频代码 如下图所示
/**
* Store recorded video
*/
protected SavedRecording save() throws IOException {
String fileName = new SimpleDateFormat("'screen-'yyyyMMdd-HHmmss'.mp4'")
.format(new Date());
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
ContentResolver resolver = mContext.getContentResolver();
Uri collectionUri = MediaStore.Video.Media.getContentUri(
MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri itemUri = resolver.insert(collectionUri, values);
Log.d(TAG, itemUri.toString());
if (mAudioSource == MIC_AND_INTERNAL || mAudioSource == INTERNAL) {
try {
Log.d(TAG, "muxing recording");
File file = File.createTempFile("temp", ".mp4",
mContext.getCacheDir());
mMuxer = new ScreenRecordingMuxer(MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4,
file.getAbsolutePath(),
mTempVideoFile.getAbsolutePath(),
mTempAudioFile.getAbsolutePath());
mMuxer.mux();
mTempVideoFile.delete();
mTempVideoFile = file;
} catch (IOException e) {
Log.e(TAG, "muxing recording " + e.getMessage());
e.printStackTrace();
}
}
// Add to the mediastore
OutputStream os = resolver.openOutputStream(itemUri, "w");
Files.copy(mTempVideoFile.toPath(), os);
os.close();
if (mTempAudioFile != null) mTempAudioFile.delete();
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
Size size = new Size(metrics.widthPixels, metrics.heightPixels);
SavedRecording recording = new SavedRecording(itemUri, mTempVideoFile, size);
mTempVideoFile.delete();
return recording;
}
到这里基本结束了,转载请注明出处Android高通 12/13 录屏流程代码位置-CSDN博客,谢谢!