0

I have develop a audio recorder which is working perfectly in foreground as well as background.But there is a problem when i am running audio recorder in background and start another app which used the sound(like video recorder, music player, video player etc) so the app will crash. So i want to add a functionality in audio recorder that when the other app (like video recorder, music player, video player etc) which are using microphone or speaker are running in foreground so audio recorder automatically go to the pause state.Thank in advance.

If u are still not understand my problem so please used your mobile and play the music player than go to the home. So the music player is running.And open the another app which are not using mic & speaker so music player is still running but when u r open another app which are using mic & speaker(like video recorder, music player, video player etc) so music player will pause. This type functionality i wanted.Thanx in advance

  • Is your audio recorder made with Activities or Service classes? – ThaMe90 Jul 31 '12 at 07:39
  • Your question is totally unclear. To pause another app you can send an intent to it and handle it in a way you'd like – Anton Jul 31 '12 at 07:40
  • Anton what is unclear in my question. tell me. and you said send an intent so sir another app is not created by me. so first understanding my question use your mobile(any os) and start a musicplayer & press the back button. So the music player is running. But open another app(like vedio player, vedio recorder and audio recorder ) so music player will paused. now i think u understand my question. – Aashutosh Shrivastava Jul 31 '12 at 07:45

2 Answers2

1

Finally lot of search & googling i have got the answer of this question. I am using audiofocuschangelistener. The code is there

 AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {

        public void onAudioFocusChange(int focusChange) {
            switch(focusChange)
            {
            case AudioManager.AUDIOFOCUS_GAIN:
            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                  //resume your activity
                   break;
            case AudioManager.AUDIOFOCUS_LOSS:
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                   ///pause your activity
                   break;
            }

        }
    };      

and do this in onCreate()

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                    //start your activity
                }

And for more information u can follow below link

http://developer.android.com/training/managing-audio/audio-focus.html

0

As far as I understood from your question, you need your application to stop whatever it's doing (sound recording probably?) when it's not the foreground application.

You need to override onPause method in your main activity so that when your activity goes into the background (the new activity comes) you can stop your recorder.

Furthermore, you can resume your activity's task when user starts interacting with your application again by overriding onResume method.

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
  • thankyou for your answer but this solution is not work. I am already do it. I think you are not understand my question. I will updated it – Aashutosh Shrivastava Jul 31 '12 at 07:49
  • can you write the code of onPause or onResume method or give me some guidance. Thankyou – Aashutosh Shrivastava Jul 31 '12 at 07:54
  • I probably did not fully understand your question. It would be great if you can update it and clarify what you are asking – Korhan Ozturk Jul 31 '12 at 07:54
  • ok. i will explain with an example. In your mobile when you play a music player & go to home so it is running in background. but when u open any app like(video recorder, audio recorder, video player) so your music player is gone to the pause state & the new app is running. Also similar when phone is coming so your music player is gone to pause state & after disconnected the phone music player is resume(it is only for example purpose i am already created this functionality in my app). – Aashutosh Shrivastava Jul 31 '12 at 08:00
  • You'd better take a look at [this question](http://stackoverflow.com/questions/151777/saving-activity-state-in-android). – Korhan Ozturk Jul 31 '12 at 08:19
  • sorry this is not helpful for me because it is already done in my app. It go to the background & come back in foreground in same state( running state).But thankyou for your help i am searching the answer of this question & if i will found it so i will post here. Thankyou for your help – Aashutosh Shrivastava Jul 31 '12 at 08:29