1
\$\begingroup\$

I'm developing a game for Android using LibGDX in Android Studio. I recently found out that some sounds in my game fail to play. I noticed that every time a sound fails to play, these are the logs:

04-12 10:49:51.784 24234-24283/com.gadarts.parashoot.android E/AudioTrack: AudioFlinger could not create track, status: -12
04-12 10:49:51.785 24234-24283/com.gadarts.parashoot.android E/SoundPool: Error creating AudioTrack
04-12 10:49:52.503 24234-24283/com.gadarts.parashoot.android W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 44100 Hz, output 48000 Hz

This is the code I use to play the sounds:

public long playSound(SFX fileName, boolean loop, float volume, boolean randomPitch) {
        if (GameSettings.SOUND_TOGGLE) {
            try {
                Sound sound = prepareSound(fileName);
                if (randomPitch && GameSettings.SOUND_RANDOM_PITCH) {
                    return setRandomPitch(loop, sound, volume);
                }
                if (loop) {
                    return sound.loop(volume);
                } else {
                    return sound.play(volume);
                }
            } catch (GdxRuntimeException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }
private Sound prepareSound(SFX soundFile) throws GdxRuntimeException {
        try {
            String soundPath = SFX.fileAttribute.SFX_FOLDER_NAME.getValue() + "/" + SFX.fileAttribute.SOUNDS_FOLDER_NAME.getValue() + "/" + soundFile.getParentDir().getValue() + "/" + soundFile.getValue() + "." + soundFile.getFormat().getValue();
            Sound sound;
            if (!sounds.containsKey(soundFile.getValue())) {
                sound = Main.getAssetsManager().get(soundPath, Sound.class);
                sounds.put(soundFile.getValue(), sound);
            } else {
                sound = sounds.get(soundFile.getValue());
            }
            if (sound == null) {
                throw new GdxRuntimeException("preparing sound failed - sound not found.");
            }
            return sound;
        } catch (GdxRuntimeException e) {
            throw e;
        }
    }

The sound files are in WAV format. Any ideas how to fix it? Thanks in advance!

Update: I've found out that this problem appears only on my Nexus 5X. I tried it on Xiaomi Redmi Note 4X and it didn't happen.

\$\endgroup\$
  • \$\begingroup\$ How large are your wav files? If I recall the Sound interface does not support files larger than 3 mb or something like that. \$\endgroup\$ – Charanor Apr 15 '17 at 16:33
  • \$\begingroup\$ The largest is about 800kb. \$\endgroup\$ – Gad Wissberg Apr 15 '17 at 16:45
  • \$\begingroup\$ Could you show us how you play it in your code? \$\endgroup\$ – Cedric Martens Jul 10 '17 at 16:12
  • \$\begingroup\$ Added the code used to play the sounds. \$\endgroup\$ – Gad Wissberg Jul 13 '17 at 9:57
  • \$\begingroup\$ I've found out that this problem appears only on my Nexus 5X. I tried it on Xiaomi Redmi Note 4X and it didn't happen. \$\endgroup\$ – Gad Wissberg Aug 10 '17 at 16:38
0
\$\begingroup\$

I had the same issue at an early stage of one of my projects. While not getting to know the actual cause of the issue, converting all my sound files into .OGG format worked for me.

\$\endgroup\$
  • \$\begingroup\$ I have converted all the wav files to ogg but it still happens. \$\endgroup\$ – Gad Wissberg May 2 '17 at 10:20
0
\$\begingroup\$

Check the sound files are 44.1k hz 16 bit .wav file as only certain bitrates will work in libgdx.

\$\endgroup\$
  • \$\begingroup\$ I converted all of my sounds to 44.1k hz 16bit .wav and still no luck... \$\endgroup\$ – Gad Wissberg May 4 '17 at 18:28
  • \$\begingroup\$ I had a similar issue in the game I'm working on. I used Audacity and matched the bitrate to what the error appeared to indicate was the correct bitrate and it fixed it for me. You may want 48k rather than 44.1k \$\endgroup\$ – LAMonday Feb 28 '18 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.