Android 视频刻录

来源:互联网 发布:大数据变革时代论文 编辑:程序博客网 时间:2024/05/17 22:06

  • Android 视频刻录
    • 界面设计
      • 横屏和全屏
      • 应用界面
    • 业务逻辑
      • 配置SurfaceView
      • 配置MediaRecorder对象


Android 视频刻录

可以通过调用系统自带的相机来实现,不过这样做灵活性较低,我们可以通过使用MediaRecorder对象SurfaceView控件来实现视频刻录。这里要求应用时横屏全屏显示。


界面设计

横屏和全屏

  • 横屏显示
    我们在AndroidManifest.xml文件中,为我们需要横屏显示的Activity添加相应的属性:
 android:screenOrientation="landscape"
  • 全屏显示
    在加载界面之前对Activity进行配置:
//去掉标题requestWindowFeature(Window.FEATURE_NO_TITLE);// 全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.activity_main);

应用界面

主要包含两部分,用于显示摄像头采集回显的预览画面,由SurfaceView来实现,另外就是停止和开始录制两个Button了,与相机应用类似,我们需要在帧布局嵌入相对布局来实现。
界面设计如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <SurfaceView        android:id="@+id/surfaceView"        android:layout_width="fill_parent"        android:layout_height="fill_parent" />    <RelativeLayout        android:id="@+id/layout"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:visibility="gone" >        <Button            android:id="@+id/btnRecord"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignParentRight="true"            android:layout_marginRight="10dp"            android:onClick="onClickRecord"            android:text="@string/btnRecord" />        <Button            android:id="@+id/btnStop"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignTop="@id/btnRecord"            android:layout_marginRight="30dp"            android:layout_toLeftOf="@id/btnRecord"            android:enabled="false"            android:onClick="onClickRecord"            android:text="@string/btnStop" />    </RelativeLayout></FrameLayout>

这里相对布局的visibility的属性值为gone,我们通过在MainActivity中重写onTouchEvent()方法来实现触摸显示。

@Override    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            // or layout.setVisibility(ViewGroup.VISIBLE);            layout.setVisibility(RelativeLayout.VISIBLE);        }        return super.onTouchEvent(event);    }

业务逻辑

配置SurfaceView

首先找到SurfaceView对象,通过getHolder()方法获取SurfaceHolder对象并进行相应配置:

surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);surfaceView.getHolder().setKeepScreenOn(true);

配置MediaRecorder对象

类似于MediaPlay,我们对其创建出来的对象进行相应的配置,主要有以下内容:

视频来源
声音来源
视频输出格式
视频编码格式
帧率
声音编码格式
文件输出路径
视频预览输出对象

File file = new File(SD_ROOTPATH, SYSTEMTIME + ".3gp");mediaRecorder = new MediaRecorder();mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//mediaRecorder.setVideoSize(320, 240);mediaRecorder.setVideoFrameRate(10);mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);mediaRecorder.setOutputFile(file.getAbsolutePath());mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

完成配置之后,就可以开始缓冲并开始录制了:

mediaRecorder.prepare();mediaRecorder.start();
0 0
原创粉丝点击