从这个blog我了解到,对于所有媒体应用程序,我应该请求AudioFocus并仅在授予AudioFocus时才开始播放。
am.requestAudioFocus(audioFocusChangeListener,AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN);
我正在报警申请 作为一个警报,它应该一直响,直到被解雇 - 除了在电话呼叫期间(这是合乎逻辑的)。
我可以使用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是否有用且非常有用(如果是,哪种模式更可取)?
另一个应用程序是否可以从我的应用程序中取出它,使警报静音?
答案 0 :(得分:0)
似乎,如果确定,使用音频焦点。 Othet应用程序不接受它,直到我放弃它。
答案 1 :(得分:-1)
伙计们,当警报或音乐播放器或WhatsApp视频通话或电话通话在所有这些情况下都可以正常工作时,您可以在onAudioFocusChange()方法中使用以下代码来管理音频焦点
// (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;
}
}
}