我在使用AudioFocusChangeListener时遇到问题,但仍然无法收听音频焦点更改。有人能帮我找到原因吗?这是我的工作:
在我的FM应用中,当FM在startFM中启动时,我放置requestAudioFocus()
来获取资源:
int result = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_FM,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.d(LOGTAG, "Successfull to request audioFocus listener");
} else {
Log.d(LOGTAG, "Failure to request focus listener");
}
退出onDestroy()
中的应用时,我使用abandonAudioFocus()
来释放资源。
成员变量mAudioFocusListener
如下:
private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
if (isFmOn()) {
fmOff();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
if (isFmOn()) {
Log.d(LOGTAG, "AudioFocus: FM is on, turning off");
mute();
stopFM();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
if (isFmOn()) {
Log.d(LOGTAG, "AudioFocus: FM is on, turning off");
mute();
stopFM();
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_GAIN");
if (isFmOn()) {
Log.d(LOGTAG, "AudioFocus: FM is off, turning back on");
unMute();
startFM();
}
break;
default:
Log.e(LOGTAG, "Unknown audio focus change code " + focusChange);
}
}
};
根据日志,它会打印“Successfull to request audioFocus listener”,但在闹钟开启或其他音频更改事件时不会打印任何AudioForcus
更改日志。任何人都可以弄清楚它有什么问题吗?
另一方面,我将日志放在AudioService.java和AudioManger.java中,在AudioService.java中,requestAudioFocus(): if(!mFocusStack.empty()&&(mFocusStack.peek()。mFocusDispatcher!= null)){
try {
mFocusStack.peek().mFocusDispatcher.dispatchAudioFocusChange(
-1 * focusChangeHint, // loss and gain codes are inverse of each other
mFocusStack.peek().mClientId); // This doesn't excute
} catch (RemoteException e) {
Log.e(TAG, " Failure to signal loss of focus due to "+ e);
e.printStackTrace();
}
}
,这里也不会执行: private IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub(){
public void dispatchAudioFocusChange(int focusChange, String id) {
Message m = mAudioFocusEventHandlerDelegate.getHandler().obtainMessage(focusChange, id);
mAudioFocusEventHandlerDelegate.getHandler().sendMessage(m);
}
这就是我的全部问题。有人能告诉我为什么这个听众不能听到警报事件和其他音频焦点改变的事件?顺便问一下,我怎么能成功听取这些事件?也许我错过了一些步骤或使用了错误的方法?非常感谢。
答案 0 :(得分:1)
试试这个:
private class MyService extends Service implements AudioManager.OnAudioFocusChangeListener {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.v(TAG, "AudioFocus: received AUDIOFOCUS_GAIN");
break;
default:
Log.e(TAG, "Unknown audio focus change code " + focusChange);
}
}
public IBinder onBind(Intent intent) {
return null;
}
}
和...
mAudioManager.requestAudioFocus(new MyService(), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
它适用于我。
答案 1 :(得分:0)
DeerCoder,
我相信如果您要求AudioManager.STREAM_MUSIC类型的音频焦点,您只会收到影响此特定音频流的事件。
mAudioManager.requestAudioFocus(new MyService(), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
警报和通知在单独的音频流上管理,例如AudioManager.STREAM_NOTIFICATION,AudioManager.STREAM_ALARM,AudioManager.STREAM_RING等......