Android自定义录制视频功能
程序员文章站
2023-12-18 23:32:46
android录制视频mediarecorder+surfaceview的使用方法,供大家参考,具体内容如下
先看效果图:
<1>将视频动画显示到...
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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。