Muting' Sound'而不是音乐'在libgdx中

时间:2014-06-19 13:59:52

标签: audio libgdx mute normalize

我正在使用eclipse和libgdx开发一个简单的游戏。 目前,我正在使用“音乐”代替“声音”来为我的游戏提供音效。 我做了一个按钮来静音所有的声音fx,但在“声音”而不是音乐方面有问题。

这是我目前的代码:

public static Music jump;

public static void load() {
jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav"));
}

public static void muteFX() {
lesgo.setVolume(0);

public static void normalizeFX() {
jump.setVolume(1f);


//'muteFX' and 'normalizeFX' to be called on different class

我想将其更改为'Sound'(原因是我希望它对快速点击更具响应性),所以它可能是这样的:

public static Sound jump;

public static void load() {
jump = Gdx.audio.newSound(Gdx.files.internal("data/jump.wav"));
}

/* here comes my problem, didn't know how to set mute and normal
volume for the jump sound. I know there is also a set volume method
to 'Sound' but really confused on the terms (long soundID, float volume)
Can someone make this clear to me on how to implement the long and soundID?
*/

我对libgdx以及java都很陌生。我研究了很多论坛,但仍然无法找到更清晰的解释。 任何帮助将不胜感激。

提前多多感谢! =)

2 个答案:

答案 0 :(得分:3)

一个快速的建议是使用声音的全局变量

public static VOLUME = 1.0f;

声音api允许您以特定音量播放声音,因此您可以做的是让游戏中的所有声音在需要时以全局值播放。

jump.play(VOLUME);

这样,你所有的开关都会改变音量的浮动值。

public static void muteFX(){
    VOLUME = 0.0f;
}
public static void normalizeFX(){
    VOLUME = 1.0f;
}

由于内存限制,不将Music类用于音效会符合您的最佳利益。

我希望这会有所帮助

答案 1 :(得分:1)

在文档中,它声明当play()或loop()方法成功时返回id。根据您想要实现的目标,这可能不方便。基本上你可以获得id并使用与此类似的东西:

long id = sound.play(1.0f); // play new sound and keep handle for further manipulation
sound.stop(id);             // stops the sound instance immediately
sound.setPitch(id, 2);      // increases the pitch to 2x the original pitch

当我想要一个按钮音量关闭和音量开启时,我有一个全局变量,我会在播放声音之前检查一些类似于此的声音:

在我的主要游戏课程中:

public static boolean soundEnabled;

每当我想播放声音时。

if (MyMainClass.soundEnabled)
sound.play();

如果你想要对声音进行任何其他控制,那么你就不可能获得声音的身份。