android 摄像头进程占用,Android 录制视频 Camera占用的解决方法

Android 录制视频 Camera占用的解决方法

最近需要修改项目中的视频录制功能,原来没怎么理会,可以正常录制。

但是不录制直接返回Camera就会出现占用情况,而且连系统相机都无法正常使用了。

只能重启手机,调试起来也比较麻烦。

最后发现 由于不当的调用

camera.lock()和camera.unlock()导致的

看一看这两个方法的解释

/**

* Re-locks the camera to prevent other processes from accessing it.

* Camera objects are locked by default unless {@link #unlock()} is

* called. Normally {@link #reconnect()} is used instead.

*

*

Since API level 14, camera is automatically locked for applications in

* {@link android.media.MediaRecorder#start()}. Applications can use the

* camera (ex: zoom) after recording starts. There is no need to call this

* after recording starts or stops.

*

*

If you are not recording video, you probably do not need this method.

*

* @throws RuntimeException if the camera cannot be re-locked (for

* example, if the camera is still in use by another process).

*/

public native final void lock();

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* Re-locks the camera to prevent other processes from accessing it.

* Camera objects are locked by default unless {@link #unlock()} is

* called.  Normally {@link #reconnect()} is used instead.

*

*

Since API level 14, camera is automatically locked for applications in

* {@link android.media.MediaRecorder#start()}. Applications can use the

* camera (ex: zoom) after recording starts. There is no need to call this

* after recording starts or stops.

*

*

If you are not recording video, you probably do not need this method.

*

* @throws RuntimeException if the camera cannot be re-locked (for

*     example, if the camera is still in use by another process).

*/

publicnativefinalvoidlock();

lock()方法是一个native方法

注释的大概意思是

调用这个方法重新锁定相机以防止其他进程访问它。系统默认的状态就是locked状态,可以调用unlock()或者reconnect()来使用

还有注释说API 14 之后当你调用了MediaRecorder.start()方法lock()会自动调用。这就解释了为什么正常录制视频不会占用相机

看看另外一个方法

/**

* Unlocks the camera to allow another process to access it.

* Normally, the camera is locked to the process with an active Camera

* object until {@link #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 {@link #reconnect()} to reclaim the camera.

*

*

This must be done before calling

* {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be

* called after recording starts.

*

*

If you are not recording video, you probably do not need this method.

*

* @throws RuntimeException if the camera cannot be unlocked.

*/

public native final void unlock();

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

* Unlocks the camera to allow another process to access it.

* Normally, the camera is locked to the process with an active Camera

* object until {@link #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 {@link #reconnect()} to reclaim the camera.

*

*

This must be done before calling

* {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be

* called after recording starts.

*

*

If you are not recording video, you probably do not need this method.

*

* @throws RuntimeException if the camera cannot be unlocked.

*/

publicnativefinalvoidunlock();

这个注释写的很清楚了

调用unlock()方法允许其他进程使用,同时正在使用Camera是locked状态

还有调视频录制的时候必须先调unlock()然后在调MediaRecorder.setCamera(Camera)

总结

视频录制的步骤如下:

调用unlock()方法

调用MediaRecorder.setCamera(Camera)方法

开始录制

停止录制

释放MediaRecorder

调用lock()方法

释放Camera

这就是完整的步骤了。

这里有个Demo可以参考一下GITHUB

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值