手机录制视频demo

public class VideoActivity extends Activity {

    
    private File myRecAudioFile;
    private SurfaceView mSurfaceView;   
    private SurfaceHolder mSurfaceHolder; 
    private Button buttonStart;
    private Button buttonStop;
    private File dir;
    private MediaRecorder recorder;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video);
        mSurfaceView = (SurfaceView) findViewById(R.id.videoView);   
  
        mSurfaceHolder = mSurfaceView.getHolder();   
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        buttonStart=(Button)findViewById(R.id.start);
        buttonStop=(Button)findViewById(R.id.stop);

        
        
        
         dir = Environment.getExternalStorageDirectory();
         recorder = new MediaRecorder();
        buttonStart.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                recorder();
                
            }
        });
        buttonStop.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 recorder.stop();
                 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
                 recorder.release();
                 recorder=null;
                 
                 dialog();
            }
        });
    }
    public void recorder() {
        try {
//             dir = Environment.getExternalStorageDirectory();
//            MediaRecorder recorder = new MediaRecorder();
            myRecAudioFile = File.createTempFile("video", ".3gp",
                    dir);
            //myRecAudioFile = File.createTempFile("video", ".mp4",
                //    dir);
            
            recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            // 设置录音源为麦克风
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置输出格式为3gp
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            // 设置视频大小
            recorder.setVideoSize(176, 144);
            //recorder.setVideoSize(400, 350);
            recorder.setVideoFrameRate(15);
            // 设置视频编码
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
            // 设置音频编码
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setMaxDuration(10000);
            recorder.setOutputFile(myRecAudioFile.getAbsolutePath());
            recorder.prepare();
            recorder.start();
            
            
            
//            WindowManager wm = (WindowManager) getSystemService(VideoActivity.this.WINDOW_SERVICE);//获取窗口服务
//            Display display = wm.getDefaultDisplay();//获取屏幕信息
//            recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
//            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
//            recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
//            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//            recorder.setVideoSize(display.getWidth(), display.getHeight()); //大小为屏幕的宽和高
//            recorder.setVideoFrameRate(3); //每秒3帧
//            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式
//            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//            recorder.setOutputFile(myRecAudioFile.getAbsolutePath());
//             recorder.prepare();//预期准备
//             recorder.start();   
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 public void dialog(){
     
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage("是否立刻上传录制的视频?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    ContentDialog.LoginDialog(VideoActivity.this).show();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
 }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">  
<SurfaceView android:id="@+id/videoView"
        android:visibility="visible" android:layout_width="400px" android:layout_height="210px">
</SurfaceView>
<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">  
<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="录制"
  android:id="@+id/start">  
></Button>
<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/start"
  android:text="停止"
  android:id="@+id/stop">  
></Button>
</RelativeLayout>
</LinearLayout>
原文地址:https://www.cnblogs.com/vus520/p/2561925.html