MediaRecorder主要用于录制音频和视频的,整个录制过程的状态机如下:
以下是使用MediaRecorder进行视频录制的一个简单的流程:
MediaRecorder recorder = new MediaRecorder(context);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
MediaRecorder.setOutputFormat(int)
,设置视频的输出格式,支持一下几种格式:
OutputFormat.DEFAULT,
OutputFormat.THREE_GPP,
OutputFormat.MPEG_4,
OutputFormat.AMR_NB,
OutputFormat.AMR_WB,
OutputFormat.AAC_ADIF,
OutputFormat.AAC_ADTS,
OutputFormat.MPEG_2_TS,
OutputFormat.WEBM,
OutputFormat.HEIF,
OutputFormat.OGG,
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);设置音频的格式,音频格式支持以下几种:
AudioSource.DEFAULT,
AudioSource.MIC,
AudioSource.VOICE_UPLINK,
AudioSource.VOICE_DOWNLINK,
AudioSource.VOICE_CALL,
AudioSource.CAMCORDER,
AudioSource.VOICE_RECOGNITION,
AudioSource.VOICE_COMMUNICATION,
AudioSource.UNPROCESSED,
AudioSource.VOICE_PERFORMANCE,
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)设置视频编码的格式
VideoEncoder.DEFAULT,
VideoEncoder.H263,
VideoEncoder.H264,
VideoEncoder.MPEG_4_SP,
VideoEncoder.VP8,
VideoEncoder.HEVC,
参考文献:https://developer.android.google.cn/reference/android/media/MediaRecorder?hl=en