Audio 静音';声音';而不是';音乐';在libgdx中

Audio 静音';声音';而不是';音乐';在libgdx中,audio,libgdx,mute,normalize,Audio,Libgdx,Mute,Normalize,我正在使用eclipse和libgdx开发一个简单的游戏。 目前,我正在使用“音乐”而不是“声音”作为游戏的音效。 我制作了一个按钮,用于静音所有的声音效果,但当涉及到“声音”而不是音乐时出现了问题 以下是我当前的代码: public static Music jump; public static void load() { jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav")); } public static voi

我正在使用eclipse和libgdx开发一个简单的游戏。 目前,我正在使用“音乐”而不是“声音”作为游戏的音效。 我制作了一个按钮,用于静音所有的声音效果,但当涉及到“声音”而不是音乐时出现了问题

以下是我当前的代码:

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
我想把它改成“声音”(原因是我想让它在快速点击时反应更灵敏),所以它可以是这样的:

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都是新手。我研究了很多论坛,仍然找不到更清晰的解释。 任何帮助都将不胜感激


提前多谢!=)

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

public static VOLUME = 1.0f;
sound api允许您以一定的音量播放声音,因此您可以在需要时将游戏中的所有声音设置为该全局值

jump.play(VOLUME);
这样,您的开关所要做的就是更改音量的浮动值

public static void muteFX(){
    VOLUME = 0.0f;
}
public static void normalizeFX(){
    VOLUME = 1.0f;
}
由于内存限制,最好不要使用音乐课程进行声音效果


我希望这会有所帮助,一个快速的建议是使用一个全局变量来表示声音

public static VOLUME = 1.0f;
sound api允许您以一定的音量播放声音,因此您可以在需要时将游戏中的所有声音设置为该全局值

jump.play(VOLUME);
这样,您的开关所要做的就是更改音量的浮动值

public static void muteFX(){
    VOLUME = 0.0f;
}
public static void normalizeFX(){
    VOLUME = 1.0f;
}
由于内存限制,最好不要使用音乐课程进行声音效果


我希望这有助于

在文档中,它声明在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
当我想关闭音量按钮并打开音量按钮时,我有一个全局变量,我会在播放声音之前检查它,类似于:

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();

如果您想要对声音进行任何其他控制,则不可避免地需要获取声音的id。

在文档中,它声明在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
当我想关闭音量按钮并打开音量按钮时,我有一个全局变量,我会在播放声音之前检查它,类似于:

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();

如果您想要对声音进行任何其他控制,则必然需要获取声音的id。

非常感谢您的回答,但是,尝试了此方法,但我想知道为什么即使运行/按下muteFX()方法,也无法使声音静音。当它被调用播放时,仍然使用“音量=1.0f”。我的声音只是一个音效(持续1秒),不在循环中。当你调用play时,你是在传递音量,对吗?声音。播放(音量)?尝试在运行时记录卷,Gdx.app.log(“app”,“我的卷:”+VOLUME);现在解决了我的问题!我从这个答案中学到了很多。非常感谢!我的卷代码的问题是,它在运行时没有被记录。我从运行时游戏类返回了“volume=0.0f”的卷,并在解决它的静音按钮上声明它。非常感谢!=)嘿,非常感谢您的回答,不过,我尝试了这个方法,但我想知道为什么即使我运行/按下muteFX()方法,也无法使声音静音。当它被调用播放时,仍然使用“音量=1.0f”。我的声音只是一个音效(持续1秒),不在循环中。当你调用play时,你是在传递音量,对吗?声音。播放(音量)?尝试在运行时记录卷,Gdx.app.log(“app”,“我的卷:”+VOLUME);现在解决了我的问题!我从这个答案中学到了很多。非常感谢!我的卷代码的问题是,它在运行时没有被记录。我从运行时游戏类返回了“volume=0.0f”的卷,并在解决它的静音按钮上声明它。非常感谢!=)感谢您对soundID和long的清晰解释。你说得对,用在我的箱子上不方便。使用了一个全局变量,现在一切正常。谢谢!感谢您对soundID和long的清晰解释。你说得对,用在我的箱子上不方便。使用了一个全局变量,现在一切正常。谢谢!