android 录像机

来源:互联网 发布:linux 大于1g的文件 编辑:程序博客网 时间:2024/04/28 14:03

一直都做camera 录像功能其实知道的很少,以前也是迷迷糊糊知道怎么写个video,今天测试了一下,各种问题。问题来源首先是对于SDK的阅读不够仔细。

实践的比较少。


其实所谓的录像 就是两个类的结合 一个是Camera 一个是MediaRecorder 这两个类搞好了,轻松搞定。我用最简洁的代码完成录制功能。


代码在后面给出下载地址。

如果代码在你的手机上运行有问题,可能有以下几种可能。

1,保存路径那里可能有问题,因为我拿的机子是山寨机。

你可以更改getName()函数来更改你的存储路径。

2,mCamcorderProfile的获取有问题,你可以添加判断,参考

CamcorderProfile的SDK 来获取这个实例。

第一部首先要让camera处于预览状态。


SDK上写的很明显 

<uses-permission android:name="android.permission.CAMERA" /><uses-feature android:name="android.hardware.camera" />
先给出,如果要往SD卡上录制文件 还需要 另外两个权限

<uses-permission
    android:name="android.permission.RECORD_AUDIO">
</uses-permission>
<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>

在此感谢http://blog.csdn.net/lissdy/article/details/7039332 。为我提供了思路。


To take pictures with this class, use the following steps:

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
  4. If desired, call setDisplayOrientation(int).
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
上面六条为google SDK 上 进入到预览的过程。 


下面开始录制部分的分析

  1. Obtain and initialize a Camera and start preview as described above.
  2. Call unlock() to allow the media process to access the camera.
  3. Pass the camera to setCamera(Camera). See MediaRecorder information about video recording.
  4. When finished recording, call reconnect() to re-acquire and re-lock the camera.
  5. If desired, restart preview and take more photos or videos.
  6. 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(CamcorderProfile profile) 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, call reconnect() to re-acquire and re-lock the camera.

关于reconnect()在上面说过了这里就不解释了。

最后记得释放资源 release()

下面就没啥说的了。


源码下载链接


下载代码的链接稍后提供 http://download.csdn.net/detail/shen332401890/5272982