一般播放动态的画面就可以用到该控件SurfaceView:
视频刻录和音频的刻录用的都是同一个MediaRecorder的类 具体代码如下:
1 package com.android.hzy.videorecoder; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.media.MediaRecorder; 7 import android.os.Bundle; 8 import android.os.Environment; 9 import android.view.SurfaceHolder; 10 import android.view.SurfaceView; 11 import android.view.View; 12 import android.view.Window; 13 import android.view.WindowManager; 14 import android.widget.Button; 15 16 public class MainActivity extends Activity { 17 18 private SurfaceView surfaceview; 19 private MediaRecorder mr; 20 private Button bt_start; 21 private Button bt_stop; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 requestWindowFeature(Window.FEATURE_NO_TITLE); // 去除标题 必须在setContentView()方法之前调用 27 setContentView(R.layout.activity_main); 28 29 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 设置全屏 30 31 surfaceview = (SurfaceView) findViewById(R.id.surfaceview); 32 SurfaceHolder holder = surfaceview.getHolder(); 33 holder.setFixedSize(176, 154); 34 holder.setKeepScreenOn(true); 35 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 36 37 bt_start = (Button) findViewById(R.id.bt_start); 38 bt_stop = (Button) findViewById(R.id.bt_stop); 39 40 41 } 42 43 /** 44 * 开始刻录 45 * @param v 46 */ 47 public void shoot(View v){ 48 try { 49 mr = new MediaRecorder(); 50 mr.reset(); // 重置 51 mr.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置声音的来源 52 mr.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 视频的来源是相机 53 mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 54 mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 设置音频编码格式 55 mr.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 56 File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp"); 57 mr.setOutputFile(file.getAbsolutePath()); 58 // 设置预览显示 59 mr.setPreviewDisplay(surfaceview.getHolder().getSurface()); 60 mr.prepare(); 61 mr.start(); 62 63 bt_start.setEnabled(false); 64 bt_stop.setEnabled(true); 65 66 } catch (Exception e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 70 } 71 72 /** 73 * 停止刻录 74 * @param v 75 */ 76 public void stopvideo(View v){ 77 mr.stop(); 78 bt_start.setEnabled(true); 79 bt_stop.setEnabled(false); 80 mr.release(); 81 mr = null; 82 } 83 84 85 86 }