Libgdx声音| setPitch导致Sound对象停止播放/发出噪音

时间:2016-09-21 14:45:59

标签: android libgdx

我有一个调用setPitch(id,1.0f)的Sound对象。出于某种原因,在大多数设备上,这似乎工作和声音完美(我意识到1.0f是正常的)。但是在星系音符3中,它会产生一种难看的深沉声音,然后Sound对象再次调用play方法后不再发出任何声音(每次玩家获得点时我都会播放Sound对象)。设置音高是必要的,因为我在游戏的其他部分使用相同的Sound对象。我将链接下面的代码,但我觉得这可能是一个libgdx故障/硬件问题。谢谢!

if (contact.getFixtureA().getFilterData().categoryBits == Config.CATEGORYBIT_BONUS) {
    final BonusFixtureUserData userData = (BonusFixtureUserData) contact.getFixtureA().getUserData();
    if (userData.bonus.scorable) {               
        long id = AudioManager.getAudioManager().playSound(AudioManager.getAudioManager().bonusSound);
        AudioManager.getAudioManager().bonusSound.setPitch(id, Config.pointSoundPitch * 2);
    }
}

AudioManager类(下)

public class AudioManager {

private static AudioManager audioManager;

public Sound jumpSound, deathSound, pointSound, bonusSound;

public static AudioManager getAudioManager() {
    if (audioManager == null) {
        audioManager = new AudioManager();
    }
    return audioManager;
}

private float sound_volume, music_volume;

private AudioManager() {
    sound_volume = 1f;

}

public void setAudio() {
    deathSound = (Sound) Game.assetManager.get("sound/death.mp3");
    jumpSound = (Sound) Game.assetManager.get("sound/tap.mp3");
    pointSound = (Sound) Game.assetManager.get("sound/point.mp3");
    bonusSound = (Sound) Game.assetManager.get("sound/point.mp3");
}

public long playSound(Sound sound) { //Returns soundID (Sound can play multiple times, soundID refers to which instance is playing)
    long id = sound.play();
    sound.setLooping(id, false);
    sound.setVolume(id, sound_volume);
    return id;
}

public static void dispose() {
    audioManager = null;
}

}

整个游戏中Config.pointPitchSound实际上等于1.0f和1.07f之间。当它是1.0f时,它就会出现故障。我没有尝试过其他值,但我会认为他们也会这样做。

1 个答案:

答案 0 :(得分:0)

LibGDX音频实现完全取决于Android。 Sound.setPitch实际上会调用SoundPool.setRate Android documentation of setRate

尝试将声音转换为OGG格式,众所周知,SoundPool在某些设备上播放MP3文件时会遇到问题。