前言:
公司產品有很多地方都需要上傳音訊視訊,今天抽空總結一下音訊視訊的錄製。學習的主角是MediaRecorder類。
MediaRecorder類介紹:
MediaRecorder類是Android sdk提供的一個專門用於音視訊錄製,一般利用手機麥克風採集音訊,攝像頭採集圖片資訊。
MediaRecorder主要函式:
setAudioChannels(int numChannels) 設定錄製的音訊通道數
setAudioEncoder(int audio_encoder) 設定audio的編碼格式
setAudioEncodingBitRate(int bitRate) 設定錄製的音訊編碼位元率
setAudioSamplingRate(int samplingRate) 設定錄製的音訊取樣率
setAudioSource(int audio_source) 設定用於錄製的音源
setAuxiliaryOutputFile(String path) 輔助時間的推移視訊檔案的路徑傳遞
setAuxiliaryOutputFile(FileDescriptor fd)在檔案描述符傳遞的輔助時間的推移視訊
setCamera(Camera c) 設定一個recording的攝像頭
setCaptureRate(double fps) 設定視訊幀的捕獲率
setMaxDuration(int max_duration_ms) 設定記錄會話的最大持續時間(毫秒)
setMaxFileSize(long max_filesize_bytes) 設定記錄會話的最大大小(以位元組為單位)
setOutputFile(FileDescriptor fd) 傳遞要寫入的檔案的檔案描述符
setOutputFile(String path) 設定輸出檔案的路徑
setOutputFormat(int output_format) 設定在錄製過程中產生的輸出檔案的格式
setPreviewDisplay(Surface sv) 表面設定顯示記錄媒體(視訊)的預覽
setVideoEncoder(int video_encoder) 設定視訊編碼器,用於錄製
setVideoEncodingBitRate(int bitRate) 設定錄製的視訊編碼位元率
setVideoFrameRate(int rate) 設定要捕獲的視訊幀速率
setVideoSize(int width, int height) 設定要捕獲的視訊的寬度和高度
setVideoSource(int video_source) 開始捕捉和編碼資料到setOutputFile(指定的檔案)
setLocation(float latitude, float longitude) 設定並儲存在輸出檔案中的地理資料(經度和緯度)
setProfile(CamcorderProfile profile) 指定CamcorderProfile物件
setOrientationHint(int degrees)設定輸出的視訊播放的方向提示
setOnErrorListener(MediaRecorder.OnErrorListener l)註冊一個用於記錄錄製時出現的錯誤的監聽器
setOnInfoListener(MediaRecorder.OnInfoListener listener)註冊一個用於記錄錄製時出現的資訊事件
getMaxAmplitude() 獲取在前一次呼叫此方法之後錄音中出現的最大振幅
prepare()準備錄製。
release()釋放資源
reset()將MediaRecorder設為空閒狀態
start()開始錄製
stop()停止錄製
MediaRecorder主要配置引數:
1.)視訊編碼格式MediaRecorder.VideoEncoder
default,H263,H264,MPEG_4_SP,VP8
2.)音訊編碼格式MediaRecorder.AudioEncoder
default,AAC,HE_AAC,AAC_ELD,AMR_NB,AMR_WB,VORBIS
3.)視訊資源獲取方式MediaRecorder.VideoSource
default,CAMERA,SURFACE
4.)音訊資源獲取方式MediaRecorder.AudioSource
defalut,camcorder,mic,voice_call,voice_communication,voice_downlink,voice_recognition, voice_uplink
5.)資源輸出格式MediaRecorder.OutputFormat
amr_nb,amr_wb,default,mpeg_4,raw_amr,three_gpp,aac_adif, aac_adts, output_format_rtp_avp, output_format_mpeg2ts ,webm
MediaRecorder錄製視訊簡單實現:
1.)需要新增許可權
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.)簡單實現錄製視訊
public class MediaRecorderActivity extends Activity implements SurfaceHolder.Callback { private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private Button btnStartStop; private boolean isRecording = false;//標記是否已經在錄製 private MediaRecorder mRecorder;//音視訊錄製類 private Camera mCamera = null;//相機 private Camera.Size mSize = null;//相機的尺寸 private int mCameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;//預設後置攝像頭 private static final SparseIntArray orientations = new SparseIntArray();//手機旋轉對應的調整角度 static { orientations.append(Surface.ROTATION_0, 90); orientations.append(Surface.ROTATION_90, 0); orientations.append(Surface.ROTATION_180, 270); orientations.append(Surface.ROTATION_270, 180); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setWindow(); setContentView(R.layout.activity_media_recorder); initViews(); } private void setWindow() { requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 設定全屏 // 設定豎屏顯示 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 選擇支援半透明模式,在有surfaceview的activity中使用。 getWindow().setFormat(PixelFormat.TRANSLUCENT); } private void initViews() { mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); btnStartStop = (Button) findViewById(R.id.btnStartStop); btnStartStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!isRecording) { startRecord(); } else { stopRecord(); } } }); SurfaceHolder holder = mSurfaceView.getHolder();// 取得holder holder.setFormat(PixelFormat.TRANSPARENT); holder.setKeepScreenOn(true); holder.addCallback(this); // holder加入回撥介面 } /** * 初始化相機 */ private void initCamera() { if (Camera.getNumberOfCameras() == 2) { mCamera = Camera.open(mCameraFacing); } else { mCamera = Camera.open(); } CameraSizeComparator sizeComparator = new CameraSizeComparator(); Camera.Parameters parameters = mCamera.getParameters(); if (mSize == null) { List<Camera.Size> vSizeList = parameters.getSupportedPreviewSizes(); Collections.sort(vSizeList, sizeComparator); for (int num = 0; num < vSizeList.size(); num++) { Camera.Size size = vSizeList.get(num); if (size.width >= 800 && size.height >= 480) { this.mSize = size; break; } } mSize = vSizeList.get(0); List<String> focusModesList = parameters.getSupportedFocusModes(); //增加對聚焦模式的判斷 if (focusModesList.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } else if (focusModesList.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } mCamera.setParameters(parameters); } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int orientation = orientations.get(rotation); mCamera.setDisplayOrientation(orientation); } @Override protected void onResume() { super.onResume(); initCamera(); } @Override public void onPause() { releaseCamera(); super.onPause(); } /** * 開始錄製 */ private void startRecord() { if (mRecorder == null) { mRecorder = new MediaRecorder(); // 建立MediaRecorder } if (mCamera != null) { mCamera.stopPreview(); mCamera.unlock(); mRecorder.setCamera(mCamera); } try { // 設定音訊採集方式 mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); //設定視訊的採集方式 mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //設定檔案的輸出格式 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//aac_adif, aac_adts, output_format_rtp_avp, output_format_mpeg2ts ,webm //設定audio的編碼格式 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //設定video的編碼格式 mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //設定錄製的視訊編碼位元率 mRecorder.setVideoEncodingBitRate(1024 * 1024); //設定錄製的視訊幀率,注意文件的說明: mRecorder.setVideoFrameRate(30); //設定要捕獲的視訊的寬度和高度 mSurfaceHolder.setFixedSize(320, 240);//最高只能設定640x480 mRecorder.setVideoSize(320, 240);//最高只能設定640x480 //設定記錄會話的最大持續時間(毫秒) mRecorder.setMaxDuration(60 * 1000); mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); String path = getExternalCacheDir().getPath(); if (path != null) { File dir = new File(path + "/videos"); if (!dir.exists()) { dir.mkdir(); } path = dir + "/" + System.currentTimeMillis() + ".mp4"; //設定輸出檔案的路徑 mRecorder.setOutputFile(path); //準備錄製 mRecorder.prepare(); //開始錄製 mRecorder.start(); isRecording = true; btnStartStop.setText("停止"); } } catch (Exception e) { e.printStackTrace(); } } /** * 停止錄製 */ private void stopRecord() { try { //停止錄製 mRecorder.stop(); //重置 mRecorder.reset(); btnStartStop.setText("開始"); } catch (Exception e) { e.printStackTrace(); } isRecording = false; } /** * 釋放MediaRecorder */ private void releaseMediaRecorder() { if (mRecorder != null) { mRecorder.release(); mRecorder = null; } } /** * 釋放相機資源 */ private void releaseCamera() { try { if (mCamera != null) { mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.unlock(); mCamera.release(); } } catch (RuntimeException e) { } finally { mCamera = null; } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // 將holder,這個holder為開始在onCreate裡面取得的holder,將它賦給mSurfaceHolder mSurfaceHolder = holder; if (mCamera == null) { return; } try { //設定顯示 mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); releaseCamera(); finish(); } } @Override public void surfaceCreated(SurfaceHolder holder) { // 將holder,這個holder為開始在onCreate裡面取得的holder,將它賦給mSurfaceHolder mSurfaceHolder = holder; } @Override public void surfaceDestroyed(SurfaceHolder holder) { // surfaceDestroyed的時候同時物件設定為null if (isRecording && mCamera != null) { mCamera.lock(); } mSurfaceView = null; mSurfaceHolder = null; releaseMediaRecorder(); releaseCamera(); } private class CameraSizeComparator implements Comparator<Camera.Size> { public int compare(Camera.Size lhs, Camera.Size rhs) { if (lhs.width == rhs.width) { return 0; } else if (lhs.width > rhs.width) { return 1; } else { return -1; } } } }
3.)佈局檔案
<?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:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/btnStartStop" android:layout_width="wrap_content" android:layout_height="55dip" android:layout_gravity="center" android:text="開始" tools:context=".MainActivity" /> </LinearLayout>
4.)錄製視訊質量
通過上面的例子就可以完成視訊錄製了,但是通過自己配置的引數有時錄製的視訊質量不高,所以我們可以通過配置檔案替代上面的引數配置
CamcorderProfile profile; if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)) { profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P); } else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)) { profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P); } else { profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW); } mediaRecorder.setProfile(profile);
5.)錄製音訊簡單示例
try { if (mRecorder == null) { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設定音訊採集方式 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);//設定音訊輸出格式 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//設定音訊編碼方式 } mRecorder.setOutputFile(filePath);//設定錄音檔案輸出路徑 mRecorder.prepare(); mRecorder.start(); } catch (Exception e) { }