android 录像机,android 录像机
一直都做camera 錄像功能其實知道的很少,以前也是迷迷糊糊知道怎么寫個video,今天測試了一下,各種問題。問題來源首先是對于SDK的閱讀不夠仔細。 實踐的比較少。 其實所謂的錄像 就是兩個類的結合 一個是Camera 一個是MediaRecorder 這兩個類搞好了,輕松
一直都做camera 錄像功能其實知道的很少,以前也是迷迷糊糊知道怎么寫個video,今天測試了一下,各種問題。問題來源首先是對于SDK的閱讀不夠仔細。
實踐的比較少。
其實所謂的錄像 就是兩個類的結合 一個是Camera 一個是MediaRecorder 這兩個類搞好了,輕松搞定。我用最簡潔的代碼完成錄制功能。
代碼在后面給出下載地址。
如果代碼在你的手機上運行有問題,可能有以下幾種可能。
1,保存路徑那里可能有問題,因為我拿的機子是山寨機。
你可以更改getName()函數來更改你的存儲路徑。
2,mCamcorderProfile的獲取有問題,你可以添加判斷,參考
CamcorderProfile的SDK 來獲取這個實例。
第一部首先要讓camera處于預覽狀態。
SDK上寫的很明顯
先給出,如果要往SD卡上錄制文件 還需要 另外兩個權限
android:name="android.permission.RECORD_AUDIO">
android:name="android.permission.WRITE_EXTERNAL_STORAGE">
在此感謝http://blog.csdn.net/lissdy/article/details/7039332 。為我提供了思路。
To take pictures with this class, use the following steps:
Obtain an instance of Camera from?open(int).
Get existing (default) settings with?getParameters().
If necessary, modify the returned?Camera.Parameters?object
and call?setParameters(Camera.Parameters).
If desired, call?setDisplayOrientation(int).
Important: Pass a fully initialized?SurfaceHolder?to?setPreviewDisplay(SurfaceHolder).
Without a surface, the camera will be unable to start the preview.
Important: Call?startPreview()?to
start updating the preview surface. Preview must be started before you can take a picture.
上面六條為google SDK 上 進入到預覽的過程。
下面開始錄制部分的分析
Obtain and initialize a Camera and start preview as described above.
Call?unlock()?to allow the media process
to access the camera.
Pass the camera to?setCamera(Camera).
See?MediaRecorder?information about video recording.
When finished recording, call?reconnect()?to
re-acquire and re-lock the camera.
If desired, restart preview and take more photos or videos.
Call?stopPreview()?and?release()?as
described above.
一開始我出現了很多問題 在于沒有對這段話進行認真的分析。
看第二條 unlock() 這個函數的功能是讓通過使用這個函數讓別的進程可以訪問camera。沒有調用的話,會出現以下問題
02-01 10:25:08.251: E/MediaRecorder(5545): start failed: -19 然后start fail了
看一下SDK對于unlock ()的解釋
Unlocks the camera to allow another process to access it. Normally, the camera is locked to the process with an active Camera object until release() is called. To allow rapid handoff between processes, you can call this method to release the camera temporarily
for another process to use; once the other process is done you can call reconnect() to reclaim the camera.
釋放camera 允許其他進程使用。一般來說,照相機是被鎖定到實例化后的Camera對象的,除非release()被調用,為了進程間的快速切換,你可以調用該方法來暫時釋放camera,待另一個進程使用完成后你可以通過調用reconnect()方法來收回Camera。
此時便是這個MediaRecorder要使用進程 所以要解鎖。
看第三條 setCamera(Camera)
Sets a Camera to use for recording. Use this function to switch quickly between preview and capture mode without a teardown
of the camera object.unlock()should
be called before this. Must call before prepare().
設置一個用于錄制的Camera ,使用該方法可以快速在預覽和捕捉視頻模式間快速切換(不用重新獲取一個Camera實例) unlock()必須在這個方法前被調用,而這個方法必須在MediaRecorder.prepare()前調用。
然后便是關于錄制設置的了
看到沒這個圖
reset()
setAudioSource()
setVideoSource()
setOutputFormat()
setAudioEncoder()
setVideoEncoder()
setOutputFile()
setVideoSize()
setVideoFrameRate()
setPreviewDisplay()
prepare()
start()//開始錄制
stop()//停止錄制
這個沒什么說的只要你的參數選擇正確就OK
下面我要說的是另一個類
因為MediaRecorder中有一個方法
setProfile(CamcorderProfileprofile)?Uses
the settings from a CamcorderProfile object for recording.
設置參數用的
但是它都決定哪些參數呢?
看一下SDK對于這個東西的理解
Retrieves the predefined camcorder profile settings for camcorder applications. These settings are read-only.
The compressed output from a recording session with a given CamcorderProfile contains two tracks: one for audio and one for video.
Each profile specifies the following set of parameters:
The file output format
Video codec format
Video bit rate in bits per second
Video frame rate in frames per second
Video frame width and height,
Audio codec format
Audio bit rate in bits per second,
Audio sample rate
Number of audio channels for recording.
那怎么獲取這個對象呢 舉個簡單的例子
CamcorderProfile
mCamcorderProfile = CamcorderProfile.get(CameraInfo.CAMERA_FACING_BACK, CamcorderProfile.QUALITY_LOW);
CamcorderProfile.xxxxx 你可以換成別的對應不同的數據。 但是這里返回的對象可能為空,所以做好其他的處理準備,這里我沒有特殊處理。
然后看一下setProfile()這個函數
Uses
the settings from a CamcorderProfile object for recording. This method should be called after the video AND audio sources are set,
and
before setOutputFile(). If a time lapse CamcorderProfile is used, audio related source or recording parameters are ignored.
使用這個類來設置錄像的參數,這個方法必須在
setAudioSource() ,setVideoSource() 之后調用,但是必須在setOutPutFile之前調用。如果你需要延時拍照 ,請參看CamCorderProfile這個類的SDK 內容。
然后就開始錄制了
setPreviewDisplay()
prepare()
start()//開始錄制
結束錄制stop方法 這時候請注意 因為你釋放了camera 所以你要重新獲取
上面錄制過程有說明When finished recording, callreconnect()to
re-acquire and re-lock the camera.
關于reconnect()在上面說過了這里就不解釋了。
最后記得釋放資源 release()
下面就沒啥說的了。
源碼下載鏈接
下載代碼的鏈接稍后提供?http://download.csdn.net/detail/shen332401890/5272982
總結
以上是生活随笔為你收集整理的android 录像机,android 录像机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux计算md5命令(linux计算
- 下一篇: ddos攻击游戏(ddos攻击手游)