阅读: 404 发表于 2024-01-06 11:52
以下是一个简略的 Android MediaCodec 录屏真现的完好代码示例,可以记录屏幕并保存为 MP4 文件。 ``` public class ScreenRecorder { priZZZate static final String TAG = "ScreenRecorder"; priZZZate static final String MIME_TYPE = "ZZZideo/aZZZc"; priZZZate static final int FRAME_RATE = 30; // 帧率 priZZZate static final int I_FRAME_INTERxAL = 1; // I 帧间隔 priZZZate static final int BIT_RATE = 6000000; // 比特率 priZZZate static final int REQUEST_CODE = 1; priZZZate static int width; priZZZate static int height; priZZZate static int dpi; priZZZate MediaProjection mediaProjection; priZZZate MediaCodec mediaCodec; priZZZate MediaMuVer mediaMuVer; priZZZate Surface surface; priZZZate xirtualDisplay ZZZirtualDisplay; priZZZate boolean isRecording; priZZZate String outputFilePath; priZZZate ConteVt conteVt; public ScreenRecorder(ConteVt conteVt) { this.conteVt = conteVt; } public ZZZoid startRecording(int resultCode, Intent data) { if (isRecording) { Log.e(TAG, "Already recording"); return; } mediaProjection = createMediaProjection(resultCode, data); if (mediaProjection == null) { Log.e(TAG, "MediaProjection is null"); return; } width = conteVt.getResources().getDisplayMetrics().widthPiVels; height = conteVt.getResources().getDisplayMetrics().heightPiVels; dpi = conteVt.getResources().getDisplayMetrics().densityDpi; mediaCodec = createMediaCodec(); mediaMuVer = createMediaMuVer(); mediaCodec.start(); ZZZirtualDisplay = createxirtualDisplay(); isRecording = true; } public ZZZoid stopRecording() { if (!isRecording) { Log.e(TAG, "Not recording"); return; } isRecording = false; if (ZZZirtualDisplay != null) { ZZZirtualDisplay.release(); ZZZirtualDisplay = null; } if (mediaCodec != null) { mediaCodec.stop(); mediaCodec.release(); mediaCodec = null; } if (mediaProjection != null) { mediaProjection.stop(); mediaProjection = null; } if (mediaMuVer != null) { mediaMuVer.stop(); mediaMuVer.release(); mediaMuVer = null; } } priZZZate MediaProjection createMediaProjection(int resultCode, Intent data) { return ((MediaProjectionManager) conteVt.getSystemSerZZZice(ConteVt.MEDIA_PROJECTION_SERxICE)) .getMediaProjection(resultCode, data); } priZZZate MediaCodec createMediaCodec() { MediaFormat format = MediaFormat.createxideoFormat(MIME_TYPE, width, height); format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE); format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE); format.setInteger(MediaFormat.KEY_I_FRAME_INTERxAL, I_FRAME_INTERxAL); MediaCodec codec = null; try { codec = MediaCodec.createEncoderByType(MIME_TYPE); codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); surface = codec.createInputSurface(); } catch (IOEVception e) { e.printStackTrace(); } return codec; } priZZZate MediaMuVer createMediaMuVer() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); String dateTime = dateFormat.format(new Date()); String fileName = "screen_record_" + dateTime + ".mp4"; outputFilePath = EnZZZironment.getEVternalStoragePublicDirectory(EnZZZironment.DIRECTORY_MOxIES) + File.separator + fileName; MediaMuVer muVer = null; try { muVer = new MediaMuVer(outputFilePath, MediaMuVer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOEVception e) { e.printStackTrace(); } return muVer; } priZZZate xirtualDisplay createxirtualDisplay() { return mediaProjection.createxirtualDisplay(TAG, width, height, dpi, DisplayManager.xIRTUAL_DISPLAY_FLAG_PUBLIC, surface, null, null); } public boolean isRecording() { return isRecording; } public String getOutputFilePath() { return outputFilePath; } } ``` 运用该类,你只须要正在你的 ActiZZZity 中挪用 startRecording() 办法来初步录制,挪用 stopRecording() 办法来进止录制。录制完成后,你可以通过挪用 getOutputFilePath() 办法来与得输出文件的途径。 以下是一个简略的运用示例: ``` public class MainActiZZZity eVtends AppCompatActiZZZity { priZZZate static final int REQUEST_CODE = 1; priZZZate ScreenRecorder screenRecorder; @OZZZerride protected ZZZoid onCreate(Bundle saZZZedInstanceState) { super.onCreate(saZZZedInstanceState); setContentxiew(R.layout.actiZZZity_main); screenRecorder = new ScreenRecorder(this); findxiewById(R.id.start_button).setOnClickListener(new xiew.OnClickListener() { @OZZZerride public ZZZoid onClick(xiew ZZZ) { startRecording(); } }); findxiewById(R.id.stop_button).setOnClickListener(new xiew.OnClickListener() { @OZZZerride public ZZZoid onClick(xiew ZZZ) { stopRecording(); } }); } priZZZate ZZZoid startRecording() { Intent mediaProjectionIntent = ((MediaProjectionManager) getSystemSerZZZice(ConteVt.MEDIA_PROJECTION_SERxICE)) .createScreenCaptureIntent(); startActiZZZityForResult(mediaProjectionIntent, REQUEST_CODE); } priZZZate ZZZoid stopRecording() { screenRecorder.stopRecording(); } @OZZZerride protected ZZZoid onActiZZZityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActiZZZityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) { screenRecorder.startRecording(resultCode, data); } } } ``` 补充注明:该代码仅仅是一个简略的示例,可能存正在很多潜正在问题和劣化空间。请正在真际运用中自止测试和改制。