我有一款Android游戏,当用户与之互动时会播放一些音效。我的一些用户抱怨说,当他们播放有声读物或音乐时,我的应用会降低背景音乐或书籍的音量。该应用程序使用SoundPool播放游戏音效。然而,即使声音关闭也存在问题。在应用程序的测试版本中,我甚至跳过了SoundPool的初始化,它没有任何效果。我甚至不确定在哪里寻找这个问题。我读过这篇文章,但没有真正的帮助。 https://developer.android.com/training/managing-audio/audio-focus.html
如何让我的应用放弃所有声音的权利,并允许后台应用以全音量播放?
// Create java sound pool
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//Load soundpool
AudioAttributes attrs = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
javaSoundPool = new SoundPool.Builder()
.setMaxStreams(10)
.setAudioAttributes(attrs)
.build();
} else {
//Load pre lollipop sound pool
javaSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
}
private void playJavaSoundPool(final SoundData soundData) {
final int soundPoolId;
//Random find the real map
if(soundData.soundId == RANDOM_WINNER){
final int mapId = randomWinnerSound();
soundPoolId = soundMap.get(mapId).soundPoolId;
} else {
soundPoolId = soundData.soundPoolId;
}
// Set volume
float streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// Play sound
javaSoundPool.play(soundPoolId, streamVolume, streamVolume, 1, 0, 1.0f);
}