Android自定义录制视频 Android自定义录制视频功能
路人甲他哥 人气:0想了解Android自定义录制视频功能的相关内容吗,路人甲他哥在本文为您仔细讲解Android自定义录制视频的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,录制视频,下面大家一起来学习吧。
Android录制视频MediaRecorder+SurfaceView的使用方法,供大家参考,具体内容如下
先看效果图:
<1>将视频动画显示到SurfaceView控件上
<2>使用MediaRecorder类进行视频的录制
常用的方法:
mediaRecorder.reset(); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setVideoFrameRate(3); //每秒3帧 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".3gp"); mediaRecorder.setOutputFile(videoFile.getAbsolutePath()); mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); mediaRecorder.prepare();//预期准备 mediaRecorder.start();//开始刻录 mediaRecorder.stop();//停止刻录
下面看代码:
public class MediaRecorderActivity extends AppCompatActivity { private SurfaceView sv_mediarecorder_surface; private MediaRecorder mediaRecorder; private boolean isStartAndStop=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } setContentView(R.layout.activity_mediarecorder); //获取SurfaceView sv_mediarecorder_surface = (SurfaceView) findViewById(R.id.sv_mediarecorder_surface); //实例化媒体录制器 mediaRecorder = new MediaRecorder(); } //视频录制与暂停的方法 public void startMediaCorder(View view){ Button button= (Button) view; if (!isStartAndStop) { if (mediaRecorder==null){ //实例化媒体录制器 mediaRecorder = new MediaRecorder(); } mediaRecorder.reset(); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置麦克风 //设置保存的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置编码格式 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setVideoFrameRate(3); //获取根路径 String sdPath= Environment.getExternalStorageDirectory().getAbsolutePath(); //设置保存的路径 mediaRecorder.setOutputFile(sdPath+"/taoge"+System.currentTimeMillis()+".mp4"); //将画面展示到SurfaceView mediaRecorder.setPreviewDisplay(sv_mediarecorder_surface.getHolder().getSurface()); //准备 try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } button.setText("结束"); }else{ // 为其它应用释放摄像头 mediaRecorder.release(); mediaRecorder = null; //关闭 button.setText("开始"); } isStartAndStop=!isStartAndStop; } }
加载全部内容