一、步骤分为以下几步
1:申请读写外置卡权限
2:使用MediaProjectionManager进行屏幕录制
3:使用MediaCodec进行编码
4:保存到外置卡
二、代码实现
1:申请权限,首先在AndroidManifest.xml文件中加入权限声明,其次使用下面代码申请权限
public boolean checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1);
}
return false;
}
2:获取MediaProjectionManager服务,发起录屏请求
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent captureIntent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent,100);
请求结果在onActivityResult方法中回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
mediaProjection = mediaProjectionManager.getMediaProjection
(resultCode, data);
initMediaCodec();
}
}
3:编码数据
private void initMediaCodec() {
try {
//创建H264编码对象,H264传入video/avc,H265传入video/hevc
mediaCodec = MediaCodec.createEncoderByType("video/avc");
//视频信息配置
MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, 540, 960);
//颜色
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_FRAME_RATE,15);//帧数
format.setInteger(MediaFormat.KEY_BIT_RATE,400000);//比特率
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,2);//i帧间隔
//设置配置信息给mediaCodec
mediaCodec.configure(format,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
final Surface surface = mediaCodec.createInputSurface();
new Thread(){
@Override
public void run() {
mediaCodec.start();
mediaProjection.createVirtualDisplay("screen-codec",
540,960,1,
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
surface,null,null);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
while (true){
//获取可用ByteBuffer下标
int index = mediaCodec.dequeueOutputBuffer(bufferInfo, 100000);
if(index>0){
ByteBuffer buffer = mediaCodec.getOutputBuffer(index);
byte[] outData=new byte[bufferInfo.size];
//给outData设置数据
buffer.get(outData);
//写入文件
writeContent(outData);
//释放资源
mediaCodec.releaseOutputBuffer(index,false);
}
}
}
}.start();
} catch (IOException e) {
e.printStackTrace();
}
}
4:写入外置卡
public String writeContent(byte[] array) {
char[] HEX_CHAR_TABLE = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
StringBuilder sb = new StringBuilder();
for (byte b : array) {
sb.append(HEX_CHAR_TABLE[(b & 0xf0) >> 4]);
sb.append(HEX_CHAR_TABLE[b & 0x0f]);
}
FileWriter writer = null;
try {
writer = new FileWriter(Environment.getExternalStorageDirectory()+"/result.txt", true);
writer.write(sb.toString());
writer.write("\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
最终输出结果
|