本代码可能在不用的平台上会存在一些问题。。
但是主要方法还是一样的
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" > <Button android:id="@+id/bt_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:onClick="start" android:layout_marginRight="10dp"/> <Button android:id="@+id/bt_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" android:onClick="stop" android:enabled="false" android:layout_marginRight="10dp"/> </LinearLayout> </RelativeLayout>
2、MainActivity
package com.njupt.vediorecorder; import java.io.File; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private SurfaceView surfaceview; private MediaRecorder mr; private Button bt_start; private Button bt_stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); surfaceview = (SurfaceView) findViewById(R.id.surfaceview); bt_start = (Button) findViewById(R.id.bt_start); bt_stop = (Button) findViewById(R.id.bt_stop); SurfaceHolder holder = surfaceview.getHolder(); holder.setFixedSize(176, 144); holder.setKeepScreenOn(true); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void start(View v) { try { mr = new MediaRecorder(); mr.reset(); mr.setAudioSource(MediaRecorder.AudioSource.MIC); mr.setVideoSource(MediaRecorder.VideoSource.CAMERA); mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mr.setVideoEncoder(MediaRecorder.VideoEncoder.H264); File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".3gp"); mr.setOutputFile(file.getAbsolutePath()); mr.setPreviewDisplay(surfaceview.getHolder().getSurface()); mr.prepare(); bt_start.setEnabled(false); bt_stop.setEnabled(true); mr.start(); } catch (Exception e) { e.printStackTrace(); } } public void stop(View v){ mr.stop(); bt_start.setEnabled(true); bt_stop.setEnabled(false); mr.release(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
3、AndroidManifest.xml
注册权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/>//媒体刻录 <uses-permission android:name="android.permission.CAMERA"/>//拍照 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>//以下两个是读写SD卡 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
横屏显示
android:screenOrientation="landscape"