简体   繁体   English

是否可以在锁定屏幕打开时在 Android 上录制音频?

[英]Is it possible to record audio on Android while lock screen is on?

I'm writing a sound recorder app and I'm wondering if it's possible to activate the MediaRecorder while the phone has its lock screen up.我正在编写一个录音机应用程序,我想知道是否可以在手机锁定屏幕时激活 MediaRecorder。 If so, is it just a matter of a special permission or something else?如果是这样,这只是特殊许可的问题还是其他什么问题? Thanks in advance.提前致谢。

you should try using PowerManager class with permission android.permission.WAKE_LOCK . 你应该尝试使用具有权限android.permission.WAKE_LOCK的 PowerManager类。

you can find more Here. 你可以在这里找到更多

Yes it is possible. 对的,这是可能的。 Lock screen does not affect the working of services in an application. 锁定屏幕不会影响应用程序中的服务工作。 So if you are using a service, then your recording will proceed. 因此,如果您使用的是服务,那么您的录制将继续进行。 However, if your phone is in standby, then it may not work. 但是,如果您的手机处于待机状态,则可能无法正常工作。 In that case, just grab a wakelock to keep the CPU alive and function your needs. 在这种情况下,只需抓住唤醒锁即可使CPU保持活动并满足您的需求。 However, grabbing a wakelock affects battery, so you must use it intelligently. 但是,抓住唤醒锁会影响电池,所以必须聪明地使用它。

These days (Android 11 or later), this should no longer be possible:如今(Android 11 或更高版本),这不再可能:

Note: On devices running Android 9 (API level 28) or higher, apps running in the background cannot access the microphone .注意:在运行 Android 9(API 级别 28)或更高版本的设备上,后台运行的应用程序无法访问麦克风 Therefore, your app should record audio only when it's in the foreground or when you include an instance of MediaRecorder in a foreground service.因此,您的应用应仅在前台服务或在前台服务中包含 MediaRecorder 实例时录制音频。

[1] https://developer.android.com/guide/topics/media/mediarecorder [1] https://developer.android.com/guide/topics/media/mediarecorder

Restricted access to location, camera, and microphone对位置、摄像头和麦克风的访问受限
To help protect user privacy, Android 11 (API level 30) introduces limitations to when a foreground service can access the device's location, camera, or microphone.为了帮助保护用户隐私,Android 11(API 级别 30)对前台服务何时可以访问设备的位置、摄像头或麦克风进行了限制。 When your app starts a foreground service while the app is running in the background, the foreground service has the following limitations:当您的应用程序在后台运行时启动前台服务时,前台服务有以下限制:
... ...
The foreground service cannot access the microphone or camera.前台服务无法访问麦克风或摄像头。

[2] https://developer.android.com/guide/components/foreground-services [2] https://developer.android.com/guide/components/foreground-services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM