[Android] Sound issue
See original GitHub issueIssue details
On my Android 7.0 smartphone some sounds are gone at play mission time. Usually it happened on looped “engine” sound. But also gone other random sounds like shot, explosion, collision and so on. On Android 4.4 all works fine. I solved this problem by writing my own AndroidSound class that use new way to initialize SoundPool. And also increase priority for looping sounds. I think it should be applied for all, so I suggest you to fix this:
gdx.backends.android.AndroidAudio.java constructor must be updated by this way:
public AndroidAudio (Context context, AndroidApplicationConfiguration config) {
if (!config.disableAudio) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttrib = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(config.maxSimultaneousSounds).build();
}else {
soundPool = new SoundPool(config.maxSimultaneousSounds, AudioManager.STREAM_MUSIC, 0);// srcQuality: the sample-rate converter quality. Currently has no effect. Use 0 for the default.
}
manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if (context instanceof Activity) {
((Activity)context).setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
} else {
soundPool = null;
manager = null;
}
}
Also, I recommend increase sound priority in loop(…) methods. Right now it’s equal 1 and it’s the same as used in play(…) methods. But looped sound usually must play until game logic didn’t stop it. So, it can not be removed by SoundPool inner sound manager. Therefor priority in loop(…) method must be greater then used in play(…)
gdx.backends.android.AndroidSound.java
-public long loop (float volume) and
-public long loop (float volume, float pitch, float pan)
recommend to update priority from 1 to something greater, for example 3:
int streamId = soundPool.play(soundId, leftVolume, rightVolume, 3, -1, pitch); // set priority greater(3) then used in play method(1)
Version of LibGDX and/or relevant dependencies
1.9.8
Please select the affected platforms
- Android
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Thanks! Will test it and create PR if it fixes it on my apps (which have “missing” sounds from time to time).
UPDATE It works on my projects, this is really great! I was not expecting to see a fix for these long standing sound/music issues on Android. I will create the PR for the first of your changes, a small problem is that it also requires updating the libGDX android compile sdk to 21 which is included as a local dependency inside /libs.
@KristjanP94 No, that would need to be a different PR. Please feel free to create it if you have the means to reproduce the issue and test the fix or, alternatively, provide a minimal executable so that we can test it.