android允许录屏代码demo
时间: 2023-09-11 07:05:56 浏览: 69
以下是一个简单的示例代码,可以实现在Android 5.0及以上版本中使用MediaProjection API进行屏幕录制:
```
public class ScreenRecorder {
private static final String TAG = "ScreenRecorder";
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaRecorder mMediaRecorder;
private int mScreenWidth;
private int mScreenHeight;
private int mScreenDensity;
private boolean mIsRecording;
public ScreenRecorder(MediaProjection mediaProjection, int screenWidth, int screenHeight, int screenDensity) {
mMediaProjection = mediaProjection;
mScreenWidth = screenWidth;
mScreenHeight = screenHeight;
mScreenDensity = screenDensity;
}
public boolean isRecording() {
return mIsRecording;
}
public void startRecording(String filePath) {
mMediaRecorder = new MediaRecorder();
try {
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mScreenWidth, mScreenHeight);
mMediaRecorder.setOutputFile(filePath);
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG, mScreenWidth, mScreenHeight, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null);
mMediaRecorder.start();
mIsRecording = true;
}
public void stopRecording() {
if (mMediaRecorder != null) {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
}
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
mVirtualDisplay = null;
}
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
mIsRecording = false;
}
}
```
使用该类进行屏幕录制的示例代码如下:
```
private MediaProjectionManager mMediaProjectionManager;
private ScreenRecorder mScreenRecorder;
private void startScreenRecord() {
if (mMediaProjectionManager == null) {
mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
}
if (mScreenRecorder == null) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
int screenDensity = getResources().getDisplayMetrics().densityDpi;
mScreenRecorder = new ScreenRecorder(mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, mResultData),
screenWidth, screenHeight, screenDensity);
}
if (!mScreenRecorder.isRecording()) {
mScreenRecorder.startRecording(Environment.getExternalStorageDirectory() + "/test.mp4");
}
}
private void stopScreenRecord() {
if (mScreenRecorder != null && mScreenRecorder.isRecording()) {
mScreenRecorder.stopRecording();
}
}
```
需要注意的是,使用该示例代码进行屏幕录制需要先获取悬浮窗权限和录音权限。同时,还需要在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)