mainActivit如下:
package c.c; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.pm.ActivityInfo; import android.media.MediaRecorder; import android.media.MediaRecorder.OnInfoListener; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Button; /** * Demo描述: * 利用SurfaceView拍摄视频 * * 注意: * 1 严重注意:MediaRecorder参数的设置.因手机不同而有差异 * 2 在设置MediaRecorder的参数时,应先设置: * setVideoSource(),setAudioSource(),setOutputFormat(),setVideoEncoder(),setAudioEncoder * 然后再设置其余的参数,查看方法对应的API有提示 * 3 错误:mMediaRecorder.setVideoFrameRate(20); * 改正:mMediaRecorder.setVideoFrameRate(15); * 现象:在2.3上正常,4.0上报错 * 原因:每秒12-15帧之间足以表示运动,电视是30. * */ public class MainActivity extends Activity implements SurfaceHolder.Callback{ private Button mStartButton; private Button mStopButton; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private MediaRecorder mMediaRecorder; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去掉标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 设置横屏显示 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.main); init(); } private void init(){ mStartButton=(Button) findViewById(R.id.start_button); mStartButton.setOnClickListener(new ButtonClickListenerImpl()); mStopButton=(Button) findViewById(R.id.stop_button); mStopButton.setOnClickListener(new ButtonClickListenerImpl()); mSurfaceView=(SurfaceView) findViewById(R.id.surfaceView); mSurfaceHolder=mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } private void initMediaRecorder(){ //next codes is right for 2.3 and 4.0 mMediaRecorder=new MediaRecorder(); //设置视频源 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); //设置音频源 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); //设置文件输出格式 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //设置视频编码方式 mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); //设置音频编码方式 mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //设置视频高和宽,注意文档的说明: //Must be called after setVideoSource(). //Call this after setOutFormat() but before prepare(). //设置录制的视频帧率,注意文档的说明: //Must be called after setVideoSource(). //Call this after setOutFormat() but before prepare(). mMediaRecorder.setVideoFrameRate(15); //设置预览画面 mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); //设置输出路径 mMediaRecorder.setOutputFile (Environment.getExternalStorageDirectory()+File.separator+System.currentTimeMillis()+".mp4"); mMediaRecorder.setVideoSize(800, 480); //设置视频的最大持续时间 mMediaRecorder.setMaxDuration(10000); //为MediaRecorder设置监听 mMediaRecorder.setOnInfoListener(new OnInfoListener() { public void onInfo(MediaRecorder mr, int what, int extra) { if (what==MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { System.out.println("已经达到最长录制时间"); if (mMediaRecorder!=null) { mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder=null; } } } }); } private class ButtonClickListenerImpl implements OnClickListener{ public void onClick(View v) { if (v.getId()==R.id.start_button) { try { initMediaRecorder(); mMediaRecorder.prepare(); mMediaRecorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } if (v.getId()==R.id.stop_button) { if (mMediaRecorder!=null) { mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder=null; } } } } //SurfaceHolder.Callback接口 public void surfaceCreated(SurfaceHolder holder) { System.out.println("SurfaceView---->Created"); } public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { System.out.println("SurfaceView---->Changed"); } public void surfaceDestroyed(SurfaceHolder holder) { System.out.println("SurfaceView---->Destroyed"); if (mMediaRecorder!=null) { mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder=null; } } }
main.xml如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <SurfaceView android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/start_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始" android:layout_weight="1" /> <Button android:id="@+id/stop_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:layout_weight="1" /> </LinearLayout> </LinearLayout>