0

I have an application that uses the microphone to allow a user to interact with the device. I am able to user AudioRecord to get a recorder, and this works great. My issue is that if the Samsung device goes to sleep and locks, I can keep using this instance of the recorder and with a foreground service I can keep it going indefinitely. But there are scenarios where I want to stop it, and restart it. If the device is locked I can not get an instance of the recorder again. Is this something that Samsung does specifically? Is there a setting or developer option I can enable to allow recording to start from a lock screen?

         recorder = new AudioRecord(audioSource, sampleRate, CHANNEL_CONFIG, AUDIO_FORMAT,
           bufferSizeInBytes * 2);//Since short=2bytes,bufferSizeInBytes has been doubled
Brian S
  • 3,096
  • 37
  • 55

1 Answers1

2

It is not specific to Samsung devices, but rather a general Android policy that only allows certain system-level services, such as the media player, to access the microphone while the device is locked. This is a security measure to prevent apps from recording audio without the user's knowledge or consent.

However, there are ways to work around this restriction and allow your app to access the microphone while the device is locked. One approach is to use a foreground service, as you mentioned, which is a special type of service that can run in the background with a persistent notification. This allows your app to continue running and accessing the microphone even when the device is locked.

Another option is to use the MediaRecorder class instead of the AudioRecord class. The MediaRecorder class is part of the Android media framework and has the ability to record audio from the microphone even when the device is locked. You can use this class to record audio and then save it to a file for later use.

To use the MediaRecorder class, you will need to request the RECORD_AUDIO permission in your app and then create an instance of the MediaRecorder class, configure it with the appropriate settings, and start the recording. You can then stop the recording and release the MediaRecorder instance when you are done.

Please note that even with these approaches, your app may still be subject to the limitations of the Android power management system. This system can kill background apps and services to conserve battery power, which may interrupt your app's ability to access the microphone. You can use the WakeLock class to prevent the device from going to sleep while your app is running, but this can have negative impacts on battery life. It is important to carefully consider the trade-offs and use these techniques responsibly.

  • Thanks for the answers. To hit the talking points At first I thought this was just Samsung, but I believe you are correct it's not just Samsung. I had only tested once on my Pixel, and from what I can see it sometimes works fine on the Pixel, but if I try 2-3 times in a row to create a new AudioRecord, it will eventually fail. – Brian S Dec 12 '22 at 21:02
  • Foreground Service - Yes we have a foreground service, and it has a persistent notification. For the most part this works, and we have no issues. I need to do more testing but I believe if the device has no pin or security to get in, this works just fine, but with a pin for sure it will not get a recorder – Brian S Dec 12 '22 at 21:02
  • Power Management - Our application has a WakeLock, so typically this has helped us make sure we are not killed. I guess that leaves me to try using a MediaRecorder instead of a AudioRecord. – Brian S Dec 12 '22 at 21:03
  • In looking at the difference between MediaRecorder and AudioRecord, the direct to file of MediaRecorder will be an issue. We are using Cerence for Speech Recognition, and would want the instant results of AudioRecord – Brian S Dec 12 '22 at 21:14
  • I think I might have found a solution. It would seem that in Android 11 Google introduced the microphone foregroundServiceType (API 30). This combined with the other items we did and you described allows us to keep access to the mic https://developer.android.com/guide/components/foreground-services – Brian S Dec 13 '22 at 14:26