我需要为闹钟应用程序设置哪个 AudioFocus

Which AudioFocus do I need to set for an alarm application

由此blog我了解到,对于所有媒体应用程序,我应该请求 AudioFocus 并仅在 AudioFocus 被授予时才开始播放。

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

我正在做报警应用。
作为闹钟,它应该一直响起直到关闭 - 除了在 phone 通话期间(这是合乎逻辑的)。

我可以使用 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);
    }
  };

考虑到TelephonyManager,使用AudioFocus是否有用和必要(如果是,哪种模式更可取)?

另一个应用程序可以从我的应用程序中获取它,使警报静音吗?

看来,使用音频焦点就可以了。其他应用不接受,直到我放弃。

嘿伙计们,你可以在你的 onAudioFocusChange() 方法中使用下面的代码来管理你的音频焦点,当警报或音乐播放器或 WhatsApp 视频呼叫或 Phone 呼叫出现在所有这些情况下它会工作



// (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;


        }
    }
}