一直都做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 录像机相关推荐

  1. android之android.intent.category.DEFAULT的用途和使用

    1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...

  2. android:layout_with=,android – 难以理解layout_alignWithParentIfMissing

    这仅适用于使用RelativeLayout时. 如果您将元素设置为一个其他元素,则表示该元素位于该元素的左侧. 但是如果这个元素会丢失,因为你删除它,例如它将与父对齐. 举个例子 android:la ...

  3. [Android Studio] Android Studio常用快捷键

    [Android Studio] Android Studio常用快捷键 (会持续更新)这边讲的常用快捷键是指做完Keymap到Eclipse后的,不是纯Android Studio的,这边主要讲下比 ...

  4. Android利用android:indeterminateDrawable来实现ProgressBar三种方式

    方式1:(效果为补间动画一样) [html] view plaincopyprint? <ProgressBar android:layout_width="wrap_content& ...

  5. Android之Android实现浮层的上下滑动(支持内部添加View)

    前言 我K,今天居然是情人节,对于资深的单身狗来说,简直是个噩耗,今天注定是各种秀恩爱,心塞中.... 话题到此结束,管他什么情人节,今天给大家带来的是一个浮层的上下滑动,浮层滑动时分三种状态:全部显 ...

  6. Android 解决Android的TextView和EditText换行问题

    Android 解决Android的TextView和EditText换行问题 参考文章: (1)Android 解决Android的TextView和EditText换行问题 (2)https:// ...

  7. Xamarin Android教程Android基本知识版本介绍与系统介绍

    Xamarin Android教程Android基本知识版本介绍与系统介绍 Xamarin Android教程Android基本知识版本介绍与系统介绍,开发Andriod有时候不像iOS一样轻松,因为 ...

  8. 【Android】Android 设置Activity窗体 不显示标题和全屏显示

    [一]Android 设置Activity窗体 不显示标题 android:theme="@android:style/Theme.NoTitleBar" 1 <activi ...

  9. [Android]《Android艺术开发探索》第一章读书笔记

    1. 典型情况下生命周期分析 (1)一般情况下,当当前Activity从不可见重新变为可见状态时,onRestart方法就会被调用. (2)当用户打开新的Activity或者切换到桌面的时候,回调如下 ...

最新文章

  1. java 使用gdal_java-gdal实现shp转geojson
  2. C#学习笔记——数据库篇(1)
  3. pythonjam怎么运行_第二十四天 PYTHON学习
  4. 图论-欧拉图-欧拉回路-Euler-Fluery-Hierholzer-逐步插入回路法-DFS详解-并查集
  5. QString转char*的问题
  6. 动态分区添加的新字段无法插入数据
  7. 使用Nlog记录日志到数据库
  8. log4j.xml按照日期生成_荐读 | 进项发票快速生成凭证!这个功能太方便了!
  9. 单片机(MCU)最强科普(万字总结,值得收藏)
  10. 《沃顿商学院谈判课》读书笔记
  11. python编程自学网-python自学网
  12. Python实战:利用正则表达式(requests模块)获取电影排行榜
  13. matlab xcorr lags,[转载]matlab中xcorr的用法
  14. EXCEL 打印设置公共表头
  15. Windows 10 磁盘重新分区
  16. Xshell简单介绍与作用
  17. #110-【我也不知道这是什么鬼算法】Ska Piggy Banks
  18. 2018年5月出海记录(HYPACK、SES2000、Klein3000)
  19. GitHub秒变GayHub
  20. 爬虫进阶:Scrapy 抓取 boss 直聘、拉勾心得经验

热门文章

  1. Java程序和MySQL数据库中关于小数的保存问题
  2. 冀教版五年级计算机教学计划,冀教版五年级上册教学计划资料
  3. opc调试软件_组态王和三菱OPC软件完美演绎天塔之光
  4. python收集数据程序_用一行Python代码进行数据收集探索!Python真牛逼!
  5. 上古卷轴3晨风职业_巫师3:上古卷轴5老玩家,入手巫师3,体验昆特牌版“实验室”...
  6. java实现线程的方式_java多线程实现的四种方式
  7. solr cloud 更新 solrconfig 配置_Solr各版本新特性「4.x,5.x,6.x,7.x」
  8. python文件读取写入实践_python文件写入实例分析
  9. 以外的文件 删除_原来C盘还可以删除这五个文件,难怪电脑越来越卡!
  10. html编辑器不支持自定义样式,百度编辑器自定义按钮样式问题(写在cssRules不起做用)?...