美文网首页LibGDX
LibGDX音频模块之声音特效

LibGDX音频模块之声音特效

作者: 天神Deity | 来源:发表于2017-09-07 23:10 被阅读4次

    声音特效是通常不超过几秒钟的特定游戏事件(如跳转或射击枪)的短音频(一般在10秒以内)。
    声音特效可以以各种格式存储。 Libgdx支持MP3,OGG和WAV文件。 RoboVM(iOS)目前不支持OGG文件。

    在Android上,Sound实例的大小不能超过1mb。 如果您有更大的文件,请使用Music

    Sound 接口可以代表一个声音特效实例,加载声音特效的代码如下:

    Sound sound = Gdx.audio.newSound(Gdx.files.internal("data/mysound.mp3"));
    

    这将从内部目录数据加载一个名为“mysound.mp3”的音频文件。
    一旦我们成功加载音频数据,我们可以使用以下的代码播放声音特效:

    sound.play(1.0f);//满音量播放一次音效,可多次调用
    

    我们也可以对Sound进行更细粒度的控制,每次对Sound.play()的调用都会返回一个long标识,它标识该声音实例。 使用这个句柄我们可以修改这个特定的播放实例:

    //播放新的声音并保持处理以进一步操纵
    long id = sound.play(1.0f); // play new sound and keep handle for further manipulation
    //立即停止声音实例
    sound.stop(id);             // stops the sound instance immediately
    //将音高增加到原来音高的2倍
    sound.setPitch(id, 2);      // increases the pitch to 2x the original pitch
    
    //第二次播放声音,这被视为不同的实例
    id = sound.play(1.0f);      // plays the sound a second time, this is treated as a different instance
    //将全部音量设置在左侧
    sound.setPan(id, -1, 1);    // sets the pan of the sound to the left side at full volume
     //保持声音循环
    sound.setLooping(id, true); // keeps the sound looping
    //停止循环声音
    sound.stop(id);             // stops the looping sound 
    

    请注意,这些方法现在在JavaScript / WebGL后端功能存在限制,从1.9.6开始,setPan()只有在支持和启用Flash时才能工作GwtApplicationConfiguration.preferFlash = true

    一旦你不再需要声音,请释放它:

    sound.dispose();
    

    请注意,当你释放掉声音之后仍调用声音实例会出现未定义的错误.

    相关文章

      网友评论

        本文标题:LibGDX音频模块之声音特效

        本文链接:https://www.haomeiwen.com/subject/uzmwjxtx.html