当前位置:操作系统 > 安卓/Android >>

android 随手记 摄像头录像

1 xml


   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Su易做图ceView
        android:id="@+id/videoView"
        android:layout_width="300dip"
        android:layout_height="200dip"
        android:visibility="visible" >
    </Su易做图ceView>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="录制" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/start"
            android:text="停止" />
    </RelativeLayout>

</LinearLayout>

 

 

 

2,java代码

  package prox.AndroidRecordVideoTest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidRecordVideoTest extends Activity {
    private Su易做图ceView mSu易做图ceView;
    private Su易做图ceHolder mSu易做图ceHolder;
    private Button buttonStart;
    private Button buttonStop;
    private File dir;
    private MediaRecorder recorder;
    private String path = "/sdcard/RTCameraSendRecv/";
    SimpleDateFormat format1;
    String fileName = "";
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        format1 = new SimpleDateFormat("yyyyMMddHHmmss");
        mSu易做图ceView = (Su易做图ceView) findViewById(R.id.videoView);
       
        mSu易做图ceHolder = mSu易做图ceView.getHolder();
        mSu易做图ceHolder.setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        buttonStart = (Button) findViewById(R.id.start);
        buttonStop = (Button) findViewById(R.id.stop);

       
        dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        recorder = new MediaRecorder();
       
        buttonStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startrecord();
            }
        });
       
        buttonStop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                recorder.stop();
                recorder.reset();
                recorder.release();
                recorder = null;
            }
        });
    }

    public void startrecord() {
        try {
            fileName = format1.format(new Date());
           
            recorder.setPreviewDisplay(mSu易做图ceHolder.getSu易做图ce());
           
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
       
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
       
            recorder.setVideoSize(352, 288);
       
            recorder.setVideoFrameRate(25);
           
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
           
          

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,