Reputation: 3234
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
Upvotes: 0
Views: 1849
Reputation: 21
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.
Upvotes: 2