-1

From this blog I learned that, for all media applications, I should request AudioFocus and start the playback only if the AudioFocus is granted.

am.requestAudioFocus(audioFocusChangeListener,AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN);

I am making an alarm application.
As an alarm, it should always ring until dismissed - except during phone calls (it is logical).

I can track if a call is ringing or idle by using the TelephonyManager

TelephonyManager telephonyManager =
        (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    switch (telephonyManager.getCallState()) {
      case TelephonyManager.CALL_STATE_IDLE:
        outsidePause = false;
        break;
      default:
        outsidePause = true;
        break;
    }
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override public void onCallStateChanged(int state, String incomingNumber) {
      switch (state) {

        case TelephonyManager.CALL_STATE_IDLE:
          outsidePause = false;
          Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
              Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
          android.provider.Settings.System.putInt(getContentResolver(),
              android.provider.Settings.System.SCREEN_BRIGHTNESS, (0));
          am.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
          setInnerData();
          startAlarm();
          break;
        default:
          outsidePause = true;
          vibrator.cancel();
          pauseAlarm();
          break;
      }
      super.onCallStateChanged(state, incomingNumber);
    }
  };

Taking in account the TelephonyManager, is it useful and nessesary to use the AudioFocus (if it is, which mode is preferrable)?

Can another application take it from mine, making silencing the alarm?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yarh
  • 4,459
  • 5
  • 45
  • 95

2 Answers2

0

Seems, that using Audio focus if ok. Othet application does not take it, untill I abandone it.

Yarh
  • 4,459
  • 5
  • 45
  • 95
-1

Hey guys you can use below code in your onAudioFocusChange() method to Manage your Audio Focus when Alarm or Music Player or WhatsApp video call or Phone call comes in all these scenario it will work



// (Below code) Mandatory method for onAudioFocusChange
    @Override
    public void onAudioFocusChange(int focusChange) {

        switch(focusChange)
        {

            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
           // Whenever Alarm is started command comes to this place

                notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
                    notificationManager.notify(1,notification);
                    playButtonImage.setImageResource(playbutton);
                    say = false;

                mediaPlayer.pause();
                break;

            case AudioManager.AUDIOFOCUS_GAIN :

                if(mediaPlayer.isPlaying())
                {
                    notificationView.setImageViewResource(R.id.remoteViewImageView, pause);
                    notificationManager.notify(1,notification);
                    playButtonImage.setImageResource(pause);
                    say = false;
                    mediaPlayer.start();
                }
                break;

            case AudioManager.AUDIOFOCUS_LOSS:

                if(mediaPlayer.isPlaying())
                {
                    notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);

                    notificationManager.notify(1,notification);
                    playButtonImage.setImageResource(playbutton);

                    mediaPlayer.pause();
                    say = false;
                    audioManager.abandonAudioFocus(this);

                }
                break;


        }
    }
}