Android 中使用MediaRecorder实现视频录制功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 中使用MediaRecorder实现视频录制功能相关的知识,希望对你有一定的参考价值。

设置视频录制的简易界面
<SurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_above="@+id/surface"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:id="@+id/start"
android:text="录制"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/start"
android:id="@+id/stop"
android:text="停止"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Java代码

第一部分:声明
private Button start,stop;
private SurfaceView surface;
private MediaRecorder recorder;
private boolean isRecording;//录制的状态

第二部分:加载数据
private void initView(){
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
stop.setOnClickListener(this);
start.setOnClickListener(this);
surface=(SurfaceView)findViewById(R.id.surface);
//设置横屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置半透明
getWindow().setFormat(PixelFormat.TRANSLUCENT);
//设置surfaceView,分辨率的设置
surface.getHolder().setFixedSize(1280,720);
//设置屏幕常亮
surface.getHolder().setKeepScreenOn(true)
  recorder=new MediaRecorder();

}
第三部分:开始录制
//开始录制
public void startRecording(){
//初始化
recorder.setAudiosource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//设置视频的录制参数,视频的格式
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置音频的编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置视频的编码格式
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//设置录入的视频大小
recorder.setVideoSize(176, 144);
//设置一个视频帧数
recorder.setVideoFrameRate(20);
//设置视频存放的路径
try {
File file =new File(Environment.getExternalStorageDirectory().getCanonicalPath(),"myVideo.mp4");
recorder.setOutputFile(file.getAbsolutePath());

//设置预览
recorder.setPreviewDisplay(surface.getHolder().getSurface());
//准备录制
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
//更改录制状态
isRecording=true;
}
其余代码参照前篇;
 


 




















































































以上是关于Android 中使用MediaRecorder实现视频录制功能的主要内容,如果未能解决你的问题,请参考以下文章

Android-MediaRecorder录像机(视频)

Android 中使用MediaRecorder实现视频录制功能

c_cpp 在Android NDK中使用MediaRecorder

android MediaRecorder录制音频

如何使用 mediaRecorder 流式传输 android 的内部音频+屏幕?

在 Android 中使用 MediaRecorder API 而不是 Intent 进行摄像头录制