ant1
ant1

Reputation: 21

Recording audio interrupted by notifications, calls and screen timeout

I am currently working on an Android project on which we have a audio recorder module.

I am using MediaRecorder and it's working as expected except in some annoying situations :

    mRecorder = new MediaRecorder ();
    mRecorder.setAudioSource (MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat (MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile (mFileName3GP);
    mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.AAC);
    mRecorder.setAudioEncodingBitRate (96000);
    mRecorder.setAudioSamplingRate (48000);
    mRecorder.start ();

I obviously doesn't want the audio recording to still be running during a call but on other situations I'd like my app to still perform the recording normally.

I didn't find anything pointing towards a solution. However, I am thinking about wakelocks (for screen off) and AudioFocus (for notifications and incoming calls).

Any advice ?

Thanks, ant1

Upvotes: 2

Views: 3546

Answers (1)

Jon
Jon

Reputation: 1850

If this logic is embedded into an Acitivity it will be subject to the regular lifecycle changes in Android. Even if you prevent some normal operations from happening, you are then preventing/disrupting some of the expected behavior of the device. Maybe that is what you want, but there is another way.

You could use a Service instead:

http://developer.android.com/guide/components/services.html

http://developer.android.com/reference/android/app/Service.html

http://www.vogella.com/tutorials/AndroidServices/article.html

This will run in the background, so will not be affected by normal life-cycle interruptions, but you can still communicate via BroadcastReceivers and Intents.

This requires a little extra work to handle all the communication between the two, and possibly some kind of notification for the user to pause/stop/restart recordings, or whatever makes sense in your case.

Update:

Found this article on the Android docs which imply that you should be trying to pause and start your recording along with the activity pausing and starting:

http://developer.android.com/guide/topics/media/audio-capture.html

Maybe this can help too. One comment mentioned that the microphone stops input when you make/receive a call. Maybe there are built in limitations to what you are trying to do:

Pause and Resume Audio recording in Android

Upvotes: 1

Related Questions