我在使用AudioFocusChangeListener时遇到问题,但仍然无法收听音频焦点更改。有人可以帮我找到原因吗?这是我的工作:
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);
}
}
};
AudioForcus
更改日志。谁能弄清楚这是怎么回事? 如果(!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);
}
这就是我的全部问题。谁能告诉我为什么这个收听者不能收听警报事件和其他音频焦点改变的事件?顺便说一句,我如何才能成功收听事件?也许我错过了一些步骤或使用了错误的方法?非常感谢。
最佳答案
试试这个:
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);
它对我有用。