本文主要是介绍android 使用MediaCodec(硬编)编码H264码流输出到外置卡,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、步骤分为以下几步
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方法中回调
@Overrideprotected 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/hevcmediaCodec = 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帧间隔//设置配置信息给mediaCodecmediaCodec.configure(format,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);final Surface surface = mediaCodec.createInputSurface();new Thread(){@Overridepublic 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();}
最终输出结果
这篇关于android 使用MediaCodec(硬编)编码H264码流输出到外置卡的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!