android MediaCodec 录屏实时时长功能实现完整代码
时间: 2023-07-28 12:10:03 浏览: 194
以下是一个简单的 Android MediaCodec 录屏实时时长功能的实现示例代码:
```java
private MediaRecorder mMediaRecorder;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private boolean mIsRecording = false;
private long mStartTime;
private long mEndTime;
private static final int REQUEST_CODE_SCREEN_CAPTURE = 1;
private void startScreenRecording() {
if (mIsRecording) {
return;
}
mStartTime = System.currentTimeMillis();
mMediaRecorder = new MediaRecorder();
mMediaProjection = ((MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE))
.getMediaProjection(Activity.RESULT_OK, mMediaProjectionCallback);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int screenDensity = metrics.densityDpi;
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/screen_capture.mp4");
mMediaRecorder.setVideoSize(screenWidth, screenHeight);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.prepare();
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
screenWidth, screenHeight, screenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null, null);
mMediaRecorder.start();
mIsRecording = true;
}
private void stopScreenRecording() {
if (!mIsRecording) {
return;
}
mEndTime = System.currentTimeMillis();
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaProjection.stop();
mVirtualDisplay.release();
mIsRecording = false;
long duration = mEndTime - mStartTime;
Toast.makeText(this, "Screen recording stopped. Duration: " + duration + "ms", Toast.LENGTH_SHORT).show();
}
private MediaProjection.Callback mMediaProjectionCallback = new MediaProjection.Callback() {
@Override
public void onStop() {
stopScreenRecording();
}
};
private void requestScreenCapture() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startActivityForResult(((MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE))
.createScreenCaptureIntent(),
REQUEST_CODE_SCREEN_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SCREEN_CAPTURE) {
if (resultCode == Activity.RESULT_OK) {
mMediaProjection = ((MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE))
.getMediaProjection(resultCode, data);
startScreenRecording();
} else {
Toast.makeText(this, "Screen capture permission denied.", Toast.LENGTH_SHORT).show();
}
}
}
```
在这个示例中,我们使用 MediaRecorder 和 MediaProjection API 实现了录屏功能。当 startScreenRecording() 方法被调用时,它会创建一个 MediaRecorder 对象,并调用 MediaProjection API 来获取一个 VirtualDisplay 对象。然后,它会将 MediaRecorder 的输出设置为 VirtualDisplay 的 Surface,从而将屏幕内容录制到视频文件中。最后,它会在屏幕录制结束时停止 MediaRecorder 和 MediaProjection,并计算录制时间。
请注意,为了使用 MediaProjection API,您需要在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
```
您还需要在您的 Activity 中处理 onActivityResult() 方法,以获取 MediaProjection 对象。
此外,由于 Android MediaCodec 的工作方式比较复杂,如果您需要更高级的录屏功能,建议使用第三方库,如 FFmpeg 或 AndroidFFmpeg。
阅读全文