本文為以MediaRecorder 錄製小影片的控件,可設定錄影的時間、空間大小、初始時是否開啟相機等。 此控件為組合控件,繼承自LinearLayout ,為防止出錯,需實作android.media.MediaRecorder.OnErrorListener 介面。
小錄影介面
MovieRecorderView.java
import java.io.File;import java.io.IOException;import java.util.Timer;import java.util.TimerTask; import android.content.Context;import android.content.res.TypedArray;import android.hardware.Camera ;import android.hardware.Camera.Parameters;import android.media.MediaRecorder;import android.media.MediaRecorder.AudioEncoder;import android.media.MediaRecorder.AudioSource;import android.media.MediaRecorder.OnErrorListener;import android.media.MediaRecorder.OutputFormat;import android.media.MediaRecorder.Videocoder; .VideoSource;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.widget.LinearLayout;import android.widfaceView;import android.widget.LinearLayout; import com.contron.dgyj.R;import com.contron.dgyj.common.Globals;import com.contron.dgyj.im.ImGlobal;import com.lidroid.xutils.util.LogUtils; /** * 視訊播放控制* * @author liuyinjun * * @date 2015- 2-5 */public class MovieRecorderView extends LinearLayout implements OnErrorListener { private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private ProgressBar mProgressBar; private MediaRecorder mMediaRecorder; private Camera mCamera; private TimerLister;錄製完成回呼介面private int mWidth;// 視訊解析度寬度private int mHeight;// 視訊解析度高度 private boolean isOpenCamera;// 一開始就開啟相機private int mRecordMaxTime;/// 一次拍攝最長private int mTime ;// 時間計數private File mVecordFile = null;// 檔案public MovieRecorderView(Context context) { this(context, null); } public MovieRecorderView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MovieRecorderView(Context context, AttributeSet . , attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MovieRecorderView, defStyle, 0); mWidth = a.getInteger(R.styleable.MovieRecorderView_width, 320);// 預設320 mHeight = a.getInt20R. );// 預設240 isOpenCamera = a.getBoolean(R.styleable.MovieRecorderView_is_open_camera, true);// 預設開啟mRecordMaxTime = a.getInteger(R.styleable.MovieRecorderView_record_max_time, 10);// 預設為10 LayoutInflater. layout.movie_recorder_view, this); mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mProgressBar.setMax(mRecordMaxTime);// 設定進度條最大量。 (); mSurfaceHolder.addCallback(new CustomCallBack()); mSurfaceHolder。 surfaceCreated(SurfaceHolder holder) { if (!isOpenCamera) return; try { initCamera(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Overhold public void facesur , int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (!isOpenCamera) return; freeCameraResource(); } } /** * 初始化相機* * @author liuyinjun * @date 2015-2-5 * @throws IOception * initCamera() throws IOException { if (mCamera != null) { freeCameraResource(); } try { mCamera = Camera.open(); } catch (Exception e) { e.printStackTrace(); freeCameraResource(); } if (mCamera == null) return; setCameraParams (); mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay(mSurfaceHolder); mCamera.startPreview(); mCamera.unlock(); } /** * 設定相機為垂直螢幕* * @author liuyinjun * @date 2015-2-5 */ private void setCameraam( if (mCamera != null) { Parameters params = mCamera.getParameters(); params.set("orientation", "portrait"); mCamera.setParameters(params); } } /** * 釋放攝影機資源* * @author liuyinjun * @date 2015-2-5 */ private void freeCameraResource() { if (mCamera != null) { mCamera.setPreviewCallback(null); mCamera.stopPreview(); mCamera.lock(); mCamera.release(); mCamera = null; } } private void createRecordDir() { File sampleDir = new File(Environment.Diternalternal) .separator + "im/video/"); if (!sampleDir.exists()) { sampleDir.mkdirs(); } File vecordDir = sampleDir; // 建立檔案try { mVecordFile = File.createTempFile("recording", ".mp4", vecordDir);//mp4格式LogUtils.i(mVecordFile.getAbsolutePath()); } catch (IOException e) { } } /** * 初始化* * @author liuyinjun * @date 2015-2-5 * @throws IOException */ private void initRecord() throws IOException { mMediaRecorder = new MediaRecorder(order); mMediaRec. if (mCamera != null) mMediaRecorder.setCamera(mCamera); mMediaRecorder.setOnErrorListener(this); mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); mMediaRecorder.setSourceSource(VideoSource.CAM音訊來源mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);// 視訊輸出格式mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);//音訊格式mMediaRecorder.setVideoSize(mWidth,Height);// 音訊格式mMediaRecorder.setVideoSize(mWidth,Height); 16);//這個我把它去掉了,感覺沒什麼用mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 512);// 設定幀頻率,然後就清晰了mMediaRecorder.setOrientationHint(90);// 輸出旋轉90度,保持豎屏錄製mMediaRecorder .setVideoEncoder(VideoEncoder.MPEG_4_SP);// 錄影格式//由catch (RuntimeException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 開始錄影* * @author liuyinjun * @date 2015-2-5 * @param fileName * 視訊儲存位置* @param onRecordFinishListener * 達到指定時間之後回呼介面*/ public void record(final OnRecordFinishListener onRecordFinishListener) { this.mOnRecordFinishListener = onRecordFinishListener; createRecordDir(); try { if (!isOpenCamera)// 如果未開啟相機,則開啟initCamera(); initRecord(); Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub mTimeCount++; mProgressBar.setProgress(mTimeCount);// 設定進度條if (mTimeCount == mRecordMaxTime) {// 達到指定時間,停止拍攝stop(); if (mOnRecordFinishListener != null) mOnRecordFinishListener.onRecordFinish(); } } }, 0, 1000); } catch (IOException e) { e.printStackTrace(); } } /** * 停止拍攝* * @author liuyinjun * @date 2015-2-5 * / public void stop() { stopRecord(); releaseRecord(); freeCameraResource(); } /** * 停止錄製* * @author liuyinjun * @date 2015-2-5 */ public void stopRecord() { mProgressBar.setProgress(0); if (mTimer != null) mTimer.cancel( mTimer.cancel( ); if (mMediaRecorder != null) { //設定後不會崩mMediaRecorder.setOnErrorListener(null); mMediaRecorder.setPreviewDisplay(null); try { mMediaRecorder.stop(); } catch (IllegalStateException e) { e.printStackTrace(); } cStackatch (Runtime); (); } catch (Exception e) { e.printStackTrace(); } } } /** * 釋放資源* * @author liuyinjun * @date 2015-2-5 */ private void releaseRecord() { if (mMediaRecorder != null) { mMediaRecorder.setOnE ; try { mMediaRecorder.release(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } mMediaRecorder = null; } public int getTimeCount() { return mTimeCount; } /** * @return the mVecordFile * / public File getmVecordFile() { return mVecordFile; } /** *錄製完成回呼介面* * @author liuyinjun * * @date 2015-2-5 */ public interface OnRecordFinishListener { public void onRecordFinish(); } @Override public void onError(MediaRecorder mr, int , int inttra. (mr != null) mr.reset(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }}
movie_recorder_view.xml
<?xml version="1.0" encoding="utf-8"?><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:background="@android:color/background_dark" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight ="1" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?><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:background="@android:color/background_dark" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight ="1" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout>
以上所述就是本文的全部內容了,希望大家能夠喜歡。