我在以下链接中问了一个问题,如何知道音频何时开始在 android 中播放,
我得到了一个完美的答案。我附上了它的代码。现在我的问题是当音乐开始播放时我会失去音频焦点。我的onfocuschange
听众没有对音频管理器的更改做出响应。我的代码有什么问题吗?所以我可以在音频管理器中收听所有更改(开始、暂停、停止)。感谢帮助。
我在我的服务类中实现了它。
am = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
am.getMode();
afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
Log.d("new","focus: " +focusChange+"mode "+am.getMode() +"ismusic "+am.isMusicActive());
if (focusChange == AudioManager.AUDIOFOCUS_GAIN_TRANSIENT){
Log.d("new","focus:started trans " );
}else if (focusChange == AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK){
Log.d("new","focus:started duck " );
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
Log.d("new","focus:paused " );
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
Log.d("new","focus:started " );
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
Log.d("new","focus:stoped " );
// am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
// am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};