android 视频录制小例子,android 录制视频实例 VideoRecordDemo
【實例簡介】
【實例截圖】
【核心代碼】
package com.example.videorecorddemo;
import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* 錄像線程
*
* @author bcaiw
*
*/
public class RecordThread extends Thread {
private String TAG = "RecordThread";
private MediaRecorder mediarecorder;// 錄制視頻的類
private SurfaceHolder surfaceHolder;
private long recordTime;
private SurfaceView surfaceview;// 顯示視頻的控件
private Camera mCamera;
public RecordThread(long recordTime, SurfaceView surfaceview,
SurfaceHolder surfaceHolder) {
Log.i(TAG, "RecordThread");
this.recordTime = recordTime;
this.surfaceview = surfaceview;
this.surfaceHolder = surfaceHolder;
}
@Override
public void run() {
Log.i(TAG, "run()");
/**
* 開始錄像
*/
startRecord();
/**
* 啟動定時器,到規定時間recordTime后執行停止錄像任務
*/
Timer timer = new Timer();
timer.schedule(new TimerThread(), recordTime);
}
/**
* 獲取攝像頭實例對象,可以選在前置或者后置攝像頭
*
* @return
*/
private Camera getCameraInstance(boolean facingfront) {
int facing;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
if(facingfront)
facing = Camera.CameraInfo.CAMERA_FACING_FRONT;
else
facing = Camera.CameraInfo.CAMERA_FACING_BACK;
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx ) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == facing) {
try {
cam = Camera.open(camIdx);
break;
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
return cam;
}
/**
* 開始錄像
*/
public void startRecord() {
Log.i(TAG, "startRecord()");
mediarecorder = new MediaRecorder();// 創建mediarecorder對象
mCamera = getCameraInstance(true);
// 解鎖camera
mCamera.unlock();
mediarecorder.setCamera(mCamera);
// 設置錄制視頻源為Camera(相機)
mediarecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 設置錄制文件質量,格式,分辨率之類,這個全部包括了
mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
// 設置視頻文件輸出的路徑
String filePath = Environment.getExternalStorageDirectory() "/SilentRecordService/Media/VideRecordDemo.mp4";
Log.i(TAG, "startRecord(), filePath: " filePath);
mediarecorder.setOutputFile(filePath);
/*
mediarecorder = new MediaRecorder();// 創建mediarecorder對象
// 設置錄制音頻源為Mic
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 設置錄制視頻源為Camera(相機)
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 設置錄制完成后視頻的封裝格式THREE_GPP為3gp,MPEG_4為mp4
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//
//mediarecorder.setAudioSamplingRate(DEFAULT_SAMPLING_RATE);
//
//mediarecorder.setAudioChannels(AudioFormat.CHANNEL_IN_MONO);
// 設置錄制的音頻編碼
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// 設置錄制的視頻編碼h263 h264
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// 設置視頻錄制的分辨率。必須放在設置編碼和格式的后面,否則報錯
mediarecorder.setVideoSize(176, 144);
// 設置錄制的視頻幀率。必須放在設置編碼和格式的后面,否則報錯
mediarecorder.setVideoFrameRate(15);
mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
// 設置視頻文件輸出的路徑
mediarecorder.setOutputFile(Environment.getExternalStorageDirectory() "/SilentRecordService/Media/VideRecordDemo.mp4");
*/
try {
// 準備錄制
Log.i(TAG, "mediarecorder.prepare()");
mediarecorder.prepare();
// 開始錄制
Log.i(TAG, "mediarecorder.start()");
mediarecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 停止錄制
*/
public void stopRecord() {
Log.i(TAG, "stopRecord()");
if (mediarecorder != null) {
// 停止錄制
mediarecorder.stop();
// 釋放資源
mediarecorder.release();
mediarecorder = null;
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
/**
* 定時器
*
* @author bcaiw
*
*/
class TimerThread extends TimerTask {
/**
* 停止錄像
*/
@Override
public void run() {
Log.i(TAG, "TimerThread, run()");
stopRecord();
this.cancel();
}
}
}
總結
以上是生活随笔為你收集整理的android 视频录制小例子,android 录制视频实例 VideoRecordDemo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语写作中常见语法总结(一)
- 下一篇: php 几秒后返回,php计算两个时间差