Android之录制视频文件
直接使用实例演示(注意代码注释):
【0】Manifest文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hoo.videorecord" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="com.hoo.videorecord.MainActivity" android:label="@string/app_name" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <!--添加摄像头,录音权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.CAMERA"/> <!--添加SDCard的读写权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> </manifest>
【1】主界面布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SurfaceView android:layout_width="fill_parent" android:layout_height="220dip" android:id="@+id/surfaceView" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/record" android:id="@+id/record" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:text="@string/stop" android:id="@+id/stop" /> </LinearLayout> </LinearLayout>
【2】MainActivity
public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private SurfaceView surfaceView; private MediaRecorder mediaRecorder; private boolean record; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取一个mediaRecorder实例 mediaRecorder = new MediaRecorder(); surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView); //下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //设置分辨率 this.surfaceView.getHolder().setFixedSize(320, 240); //设置按钮监听器 ButtonClickListener listener = new ButtonClickListener(); //找到录制与停止按钮 Button stopButton = (Button) this.findViewById(R.id.stop); Button recordButton = (Button) this.findViewById(R.id.record); //两个按钮的监听器 stopButton.setOnClickListener(listener); recordButton.setOnClickListener(listener); } //设置销毁 @Override protected void onDestroy() { mediaRecorder.release(); super.onDestroy(); } //设置 private final class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show(); return ; } try { switch (v.getId()) { case R.id.record: mediaRecorder.reset(); //从照相机采集视频 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置输出格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //设置大小 mediaRecorder.setVideoSize(320, 240); //每秒3帧 mediaRecorder.setVideoFrameRate(3); //设置视频编码方式 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置音频编码方式 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //把录制下的视频保存到SDCard的根目录下 File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".3gp"); mediaRecorder.setOutputFile(videoFile.getAbsolutePath()); // mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); //预期准备 mediaRecorder.prepare(); //开始刻录 mediaRecorder.start(); record = true; break; case R.id.stop: if(record) { mediaRecorder.stop(); record = false; } break; } } catch (Exception e) { Toast.makeText(MainActivity.this, R.string.error, 1).show(); Log.e(TAG, e.toString()); } } } }