Audio Focus 在 android 9 和 android 10 android 中不起作用?
Audio Focus is not working in android 9 and android 10 android?
int result = audioManager.requestAudioFocus(focusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
startMediaPlayer(songPath);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioManager.registerAudioPlaybackCallback(new AudioManager.AudioPlaybackCallback() {
@Override
public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {
super.onPlaybackConfigChanged(configs);
boolean isFoundPlayOtherMusic = false;
if (configs.size() > 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (configs != null) {
for (int i = 0; i < configs.size(); i++) {
if (configs.get(i).getAudioAttributes().getUsage() == USAGE_MEDIA && (configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_MUSIC || configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_UNKNOWN)) {
isFoundPlayOtherMusic = true;
}
}
if (isFoundPlayOtherMusic) {
controls.pauseControl(getApplicationContext());
} else {
if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
controls.playControl(getApplicationContext());
}
}
}
} else {
controls.pauseControl(getApplicationContext());
}
} else if (configs.size() == 0) {
if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
controls.playControl(getApplicationContext());
}
}
}, handler);
}
这里是检查音频管理器音频焦点的代码,音频管理器是否获得成功焦点然后播放音乐播放器,我也实现了 audioChangeFocusListener 如下代码。
private AudioManager.OnAudioFocusChangeListener focusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK : " + focusChange);
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT : " + focusChange);
break;
case AudioManager.AUDIOFOCUS_GAIN:
System.out.println("===== AUDIOFOCUS_GAIN : " + focusChange);
if (!mp.isPlaying())
controls.playControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS:
System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
if (mp.isPlaying())
controls.pauseControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
System.out.println("===== AUDIOFOCUS_LOSS_TRANSIENT : " + focusChange);
if (mp.isPlaying())
controls.pauseControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
}
}
};
Audio Focus 在 android 9 和 10 中都不起作用。我在我的应用程序中使用带通知的音频播放。
在我们的应用程序中音频焦点不是增益和损失,是否可以在从其他应用程序播放音频时停止我们应用程序的音乐,以及当我在我的应用程序中播放音频然后从其他应用程序停止其他音乐播放器时,是否可能那怎么办?
这也是我很头疼的一个话题。尝试添加播放虚拟音频
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioTrack audioTrack = new AudioTrack(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build(),
new AudioFormat.Builder()
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
.setSampleRate(48000)
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.build(),
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT),
AudioTrack.MODE_STREAM,
AudioManager.AUDIO_SESSION_ID_GENERATE
);
audioTrack.play();
audioTrack.stop();
audioTrack.release();
}
int result = audioManager.requestAudioFocus(focusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
startMediaPlayer(songPath);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioManager.registerAudioPlaybackCallback(new AudioManager.AudioPlaybackCallback() {
@Override
public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {
super.onPlaybackConfigChanged(configs);
boolean isFoundPlayOtherMusic = false;
if (configs.size() > 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (configs != null) {
for (int i = 0; i < configs.size(); i++) {
if (configs.get(i).getAudioAttributes().getUsage() == USAGE_MEDIA && (configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_MUSIC || configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_UNKNOWN)) {
isFoundPlayOtherMusic = true;
}
}
if (isFoundPlayOtherMusic) {
controls.pauseControl(getApplicationContext());
} else {
if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
controls.playControl(getApplicationContext());
}
}
}
} else {
controls.pauseControl(getApplicationContext());
}
} else if (configs.size() == 0) {
if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
controls.playControl(getApplicationContext());
}
}
}, handler);
}
这里是检查音频管理器音频焦点的代码,音频管理器是否获得成功焦点然后播放音乐播放器,我也实现了 audioChangeFocusListener 如下代码。
private AudioManager.OnAudioFocusChangeListener focusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK : " + focusChange);
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT : " + focusChange);
break;
case AudioManager.AUDIOFOCUS_GAIN:
System.out.println("===== AUDIOFOCUS_GAIN : " + focusChange);
if (!mp.isPlaying())
controls.playControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS:
System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
if (mp.isPlaying())
controls.pauseControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
System.out.println("===== AUDIOFOCUS_LOSS_TRANSIENT : " + focusChange);
if (mp.isPlaying())
controls.pauseControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
}
}
};
Audio Focus 在 android 9 和 10 中都不起作用。我在我的应用程序中使用带通知的音频播放。
在我们的应用程序中音频焦点不是增益和损失,是否可以在从其他应用程序播放音频时停止我们应用程序的音乐,以及当我在我的应用程序中播放音频然后从其他应用程序停止其他音乐播放器时,是否可能那怎么办?
这也是我很头疼的一个话题。尝试添加播放虚拟音频
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioTrack audioTrack = new AudioTrack(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build(),
new AudioFormat.Builder()
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
.setSampleRate(48000)
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.build(),
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT),
AudioTrack.MODE_STREAM,
AudioManager.AUDIO_SESSION_ID_GENERATE
);
audioTrack.play();
audioTrack.stop();
audioTrack.release();
}