mainActivity如下:
package c.c;import java.io.File;import java.io.IOException;import android.app.Activity;import android.content.pm.ActivityInfo;import android.hardware.Camera;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. * * 說明: * 從Android錄製視頻(五)到Android錄製視頻(一)難度 * 逐漸上升也漸漸趨於完善. * */public class MainActivity extends Activity implements SurfaceHolder.Callback{private Button mStartButton;private Button mStopButton;private Camera mCamera;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.setCamera(mCamera); //設定視頻源 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) {//mCamera.lock();mCamera.stopPreview();mMediaRecorder.stop();mMediaRecorder.release();mMediaRecorder=null;}}} } //SurfaceHolder.Callback介面public void surfaceCreated(SurfaceHolder holder) { System.out.println("SurfaceView---->Created"); try { mCamera=Camera.open();mCamera.setPreviewDisplay(mSurfaceHolder);mCamera.startPreview();mCamera.unlock();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}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>