gpt4 book ai didi

android - onAudioFocusChange 未调用

转载 作者:行者123 更新时间:2023-11-29 14:32:11 28 4
gpt4 key购买 nike

private static int changedFocus;
private static boolean focusGranted;
private static AudioManager audioManager;

public static int getChangedFocus() {
return changedFocus;
}

public static boolean isFocusGranted() {
return focusGranted;
}

public static void requestFocus(final Context context) {
if (audioManager == null) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
switch (audioManager.requestAudioFocus(OnFocusChangeListener.getInstance(),
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)) {
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
focusGranted = true;
break;

case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
focusGranted = false;
break;
}
}

private static final class OnFocusChangeListener implements AudioManager.OnAudioFocusChangeListener {

private static OnFocusChangeListener instance;

protected static OnFocusChangeListener getInstance() {
if (instance == null) {
instance = new OnFocusChangeListener();
}
return instance;
}

@Override
public void onAudioFocusChange(final int focusChange) {
changedFocus = focusChange;
System.out.println("on focus change: " + focusChange);
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
GlobalMusicPlayer.onFocusGained();
break;

case AudioManager.AUDIOFOCUS_LOSS:
GlobalMusicPlayer.onFocusLoss();
break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
GlobalMusicPlayer.onFocusLossTransient();
break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
GlobalMusicPlayer.onFocusLossTransientCanDuck();
break;
}
}
}

我调用 requestFocus(context) 并获取 AudioManager.AUDIOFOCUS_REQUEST_GRANTED 作为返回值。

在那之后,onAudioFocusChange 不再调用,因此 changedFocus 始终为 0。

isFocusGranted() // true
getChangedFocus() // 0

设备:Nexus 10、摩托罗拉 Xoom。

最佳答案

case AudioManager.AUDIOFOCUS_GAIN in onAudioFocusChange() 更像是 AudioFocus 重新增益。它仅在丢失事件后触发。

case AudioManager.AUDIOFOCUS_REQUEST_GRANTED 是您应该添加另一个 GlobalMusicPlayer.onFocusGained() 的地方。

关于android - onAudioFocusChange 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com