码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-12-14 19:12:31      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:stack   abs   init   surf   tput   cti   int   environ   preview   

设置视频录制的简易界面
<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实现视频录制功能

标签:stack   abs   init   surf   tput   cti   int   environ   preview   

原文地址:http://www.cnblogs.com/BLJworld1994/p/6180260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!