一直都做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

android 录像机相关推荐

  1. android 录像机,android 录像机

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

  2. 流媒体服务器+终端(android,ios,web),如何从海康平台上拉流接入RTSP安防网络摄像头/海康大华硬盘录像机网页无插件直播流媒体服务器EasyNVR?...

    背景需求 随着雪亮工程.明厨亮灶.手机看店.智慧幼儿园监控等行业开始将传统的安防摄像头进行互联网.微信直播,我们知道摄像头直播的春天了.将安防摄像头或NVR上的视频流转成互联网直播常用的RTMP.HT ...

  3. android 自定义录像机,android 手机录屏,最简单的 3 种方法

    原标题:android 手机录屏,最简单的 3 种方法 公众号文章不支持插入外链,下面提到的所有工具,都可点击 阅读原文前往我的个人网站 kejiweixun.com 下载. 安卓手机录制屏幕有很多方 ...

  4. android 自定义录像机,android-camera2 - 将自定义捕获请求构建器选项设置为使用Camera2 API捕获图像以使用OpenCV库进行摄像机校准 - 堆栈内存溢出...

    我们正在使用Camera2 API捕获N张图像,而未设置任何自定义捕获请求构建器选项. 我们正在使用这些图像通过OpenCV Android库411进行相机校准. 然后,我们正在使用OpenCV An ...

  5. android+锁屏录像,快速录像机(锁屏录像)

    应用介绍 快速录像机(锁屏录像)非常不错专门喜欢锁屏录像工具软件,支锁屏摄像app就是这样一款锁屏摄影软件,下载锁屏摄像app并开启后用户无需时刻开启手机屏幕也能帮你记录身边的美好生活. 基本介绍 这 ...

  6. android 自定义录像机,快速录像机(锁屏录像)

    快速录像机(锁屏录像)专门喜欢锁屏录像的小伙伴准备的非常不错的工具系列,支持这多种机型么,现在你能体验到更加舒服精彩的趣味游戏内容,相信不少的用户都会喜欢,赶紧下载吧! 基本介绍 这是一款可以帮助用户 ...

  7. android专题-蓝牙扫描、连接、读写

    android专题-蓝牙扫描.连接.读写 概念 外围设备 可以被其他蓝牙设备连接的外部蓝牙设备,不断广播自身的蓝牙名及其数据,如小米手环.共享单车.蓝牙体重秤 中央设备 可以搜索并连接周边的外围设备, ...

  8. 如何将Android带入互联网数字家庭? 第一篇转载

    前言:很有幸通过ARM Group认识了 ARM的家庭软件架构师 --- 章立(Leon Zhang) (他也是ARM战略软件联盟部门的一员. Leon 拥有多年产品开发和项目管理经验, 曾经参与了数 ...

  9. 【转】Android兼容性测试CTS Verifier-环境搭建、测试执行、结果分析

    原文网址:http://www.cnblogs.com/zh-ya-jing/p/4452675.html CTS Verifier算是CTS的一部分,需要手动进行,主要用于测试那些自动测试系统无法测 ...

最新文章

  1. window 获取进程运行长
  2. 成为Java高手的25个学习要点
  3. 贪心算法--多处最优服务次序问题
  4. OpenGL Draw TransformFeedback 绘制变换反馈的实例
  5. Java黑皮书课后题第6章:6.37(格式化整数)编写一个测试程序,提示用户输入一个数字以及宽度,显示通过调用format方法返回的字符串
  6. 15_http响应相关概念
  7. 持续集成工具 jenkins 实践
  8. python卸载_如何为Python程序制作Windows安装包?
  9. [读书]设计模式:享元模式
  10. mkfs 格式化分区并创建文件系统
  11. tf卡可以自己裁剪成nm卡_真假TF卡鉴定方法
  12. ESAPI(一)索引的操作以及数据插入
  13. iPhone如何快速设置自定义铃声?苹果手机铃声设置教程
  14. 中国银行软件中心信息技术岗(北京 )面试
  15. iOS应用性能调优的建议和技巧--中高级--王朋
  16. 程序实现汉字转换为拼音
  17. 《魔兽》遭黑客入侵 主城横尸遍野
  18. Sketchup 程序自动化(二)Ruby 基础、单位转换
  19. Android kotlin实现读取pdf和pptx文件
  20. mysql连接查询、自查询

热门文章

  1. 启动http报错AH00558:Could not reliably determine the server‘s fully qualified domain name using解决办法
  2. Dynamics 365: 推荐几个XrmToolBox中创建ER(Entity Relationship)图的插件
  3. 等保2.0通用测评要求
  4. RTT学习笔记9-IO设备模型
  5. STM32F4基于RTT使用SPI sever功能
  6. HotRing——热点感知的哈希冲突解决方案
  7. POLYV直播助手客户端软件做视频直播时如何添加画面源
  8. 推荐系统综述——只谈感受不谈理论
  9. 9秒学院部分开源产品简介
  10. 【MySQL】MySQL