2

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 :

  • When a notification sound is triggered by Android, the MediaRecorder stops
  • When receiving a phone call and even if refusing the call, the MediaRecorder stops
  • When the screen goes off
    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

ant1
  • 21
  • 1
  • 5

1 Answers1

1

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

Community
  • 1
  • 1
Jon
  • 1,820
  • 2
  • 19
  • 43
  • That might be the right approach when screen going off but will helps the recording not being stopped by a notification or an incoming call ? – ant1 Feb 14 '14 at 18:07
  • AFAIK it will not be affected by notifications or calls, but don't have enough direct experience to say for certain. – Jon Feb 14 '14 at 18:14
  • Obviously, pausing the recording any time the Activity pauses is not a solution to the stated problem, though there will be situations in which the *service* performing the audio operations will need to discontinue them and get out of the way. – Chris Stratton Feb 14 '14 at 19:42