本章音效文件都来自于公共许可: http://files.cnblogs.com/mignet/sounds.zip

在游戏中,播放背景音乐和音效是基本的功能。

Libgdx提供了跨平台的声音播放功能,支持的文件格式有:
•wav (RIFF WAVE)
•mp3 (MPEG-2 Audio Layer III)
•ogg (Ogg Vorbis)

Libgdx有2个接口来管理音乐和音效:Music和Sound.

Music通常要花更多的CPU周期,因为它在播放到声卡之前需要解析。Sound就不用了,因为Sound在加载的时候都已经解析过了。

常规用法:Sound sound = Gdx.audio.newSound(Gdx.files.internal("sound.wav"));同时记得sound.dispose(); // free allocated memory

除了Music和Sound,Libgdx还提供了底层的接口AudioDevice、AudioRecorder用来访问声卡以记录和播放原始的声音样本。

AudioDevice接口允许您直接发送PCM编码音频样本到音频设备。例如:

AudioDevice audioDevice = Gdx.audio.newAudioDevice(44100, false);//44.1 kHz

audioDevice.dispose(); // free allocated memory

发送到声卡的声音数据可以是一组浮点数或者是一组16位带符号短整形数.

void writeSamples(float[] samples, int offset, int numSamples);//offset (start),numSamples (length)
void writeSamples(short[] samples, int offset, int numSamples);

立体声意味着需要采集的样本是双倍的,因为立体声就是左声道和右声道交叉合成的。就是说44.1 kHz的采样率需要44100的单声道样本和88200的立体声样本。

AudioRecorder audioRecordedr = Gdx.audio.newAudioRecorder(44100, false);

audioRecorder.dispose(); // free allocated memory

同样的:void read(short[] samples, int offset, int numSamples);

现在,我们拥有足够的知识来生成自己的声音了,自己合成并保存,播放合成的声音测试效果,都可以实行了。

但是这样做对于很多经验丰富的程序员来讲都太高端了,哈哈。我们这里不讨论。

一个可行的解决方案来获得一些不错的音响效果是使用一个现有的自由的和开放源码的声音生成器。

程序员自己的音效生成工具->声音生成器:

sfxr

这个生成器最初是由托马斯“DrPetter“佩特森在2007年开发的,后来慢慢的又出现了几个sfxr变体版本,像bfxr, cfxr, as3sfxr

首先来看sfxr:

sfxr之所以可以很快流行并被全球的程序员们使用,是因为只需要简单的按下这个软件上面的“RANDOMIZE”按钮,就可以生成声音。

而且还提供了很多基本的音效像PICKUP/COIN, LASER/SHOOT, EXPLOSION, POWERUP, HIT/HURT, JUMP, BLIP/SELECT

生成想要的声音之后,导出.wav文件就可以用了。

官方源码:https://code.google.com/p/sfxr/

cfxr是Cocoa sfxr的缩写,是Mac OS下原生的Cocoa应用程序,专门为Cocoa写的实现版本。

官方源码:https://github.com/nevyn/cfxr/

bfxr:这个功能很丰富,可以创建更复杂的音效,有兴趣可以自己摸索。

官方源码:https://github.com/increpare/bfxr/

现在开始在我们的游戏中使用音乐和音效。先把文件添加到assert(背景音乐没有提供下载因为太大了,你可以随便用什么音乐文件代替):

和前面的资源管理提到的一样,我们使用内部类来统一管理各种资源。在Asserts中添加:

    public AssetSounds sounds;
public AssetMusic music; public class AssetSounds {
public final Sound jump;
public final Sound jumpWithFeather;
public final Sound pickupCoin;
public final Sound pickupFeather;
public final Sound liveLost; public AssetSounds(AssetManager am) {
jump = am.get("sounds/jump.wav", Sound.class);
jumpWithFeather = am.get("sounds/jump_with_feather.wav",
Sound.class);
pickupCoin = am.get("sounds/pickup_coin.wav", Sound.class);
pickupFeather = am.get("sounds/pickup_feather.wav", Sound.class);
liveLost = am.get("sounds/live_lost.wav", Sound.class);
}
} public class AssetMusic {
public final Music song01; public AssetMusic(AssetManager am) {
song01 = am.get("music/keith303_-_brand_new_highscore.mp3",
Music.class);
}
}

然后在init里添加:

public void init(AssetManager assetManager) {
this.assetManager = assetManager;
// set asset manager error handler
assetManager.setErrorListener(this);
// load texture atlas
assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
// load sounds
assetManager.load("sounds/jump.wav", Sound.class);
assetManager.load("sounds/jump_with_feather.wav", Sound.class);
assetManager.load("sounds/pickup_coin.wav", Sound.class);
assetManager.load("sounds/pickup_feather.wav", Sound.class);
assetManager.load("sounds/live_lost.wav", Sound.class);
// load music
assetManager.load("music/keith303_-_brand_new_highscore.mp3",
Music.class);
// start loading assets and wait until finished
assetManager.finishLoading();
Gdx.app.debug(TAG,
"# of assets loaded: " + assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames())
Gdx.app.debug(TAG, "asset: " + a);
TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
// enable texture filtering for pixel smoothing
for (Texture t : atlas.getTextures())
t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
// create game resource objects
fonts = new AssetFonts();
bunny = new AssetBunny(atlas);
rock = new AssetRock(atlas);
goldCoin = new AssetGoldCoin(atlas);
feather = new AssetFeather(atlas);
levelDecoration = new AssetLevelDecoration(atlas);
sounds = new AssetSounds(assetManager);
music = new AssetMusic(assetManager);
}

还记得我们在Options菜单中让用户来设置音乐音效吗,现在派上用场了。

创建新类AudioManager来管理播放和停止:

package com.packtpub.libgdx.canyonbunny.util;

import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound; public class AudioManager {
public static final AudioManager instance = new AudioManager();
private Music playingMusic; // singleton: prevent instantiation from other classes
private AudioManager() {
} public void play(Sound sound) {
play(sound, 1);
} public void play(Music music) {
stopMusic();
playingMusic = music;
if (GamePreferences.instance.music) {
music.setLooping(true);
music.setVolume(GamePreferences.instance.volMusic);
music.play();
}
} public void stopMusic() {
if (playingMusic != null)
playingMusic.stop();
} public void onSettingsUpdated() {
if (playingMusic == null)
return;
playingMusic.setVolume(GamePreferences.instance.volMusic);
if (GamePreferences.instance.music) {
if (!playingMusic.isPlaying())
playingMusic.play();
} else {
playingMusic.pause();
}
} public void play(Sound sound, float volume) {
play(sound, volume, 1);
} public void play(Sound sound, float volume, float pitch) {
play(sound, volume, pitch, 0);
} public void play(Sound sound, float volume, float pitch, float pan) {
if (!GamePreferences.instance.sound)
return;
sound.play(GamePreferences.instance.volSound * volume, pitch, pan);
}
}

修改MenuScreen:

    private void onSaveClicked() {
saveSettings();
onCancelClicked();
AudioManager.instance.onSettingsUpdated();
} private void onCancelClicked() {
btnMenuPlay.setVisible(true);
btnMenuOptions.setVisible(true);
winOptions.setVisible(false);
AudioManager.instance.onSettingsUpdated();
}

修改CanyonBunnyMain的create方法:

@Override
public void create() {
// Set Libgdx log level
Gdx.app.setLogLevel(Application.LOG_DEBUG);
// Load assets
Assets.instance.init(new AssetManager());
// Load preferences for audio settings and start playing music
GamePreferences.instance.load();
AudioManager.instance.play(Assets.instance.music.song01);
// Start game at menu screen
ScreenTransition transition = ScreenTransitionSlice.init(2,
ScreenTransitionSlice.UP_DOWN, 10, Interpolation.pow5Out);
setScreen(new MenuScreen(this), transition);
}

Libgdx自动处理在游戏暂停和返回时的音乐播放问题,所以这里不需要额外的代码修改。

继续修改WorldController:

..
public void update(float deltaTime) {
..
if (!isGameOver() && isPlayerInWater()) {
AudioManager.instance.play(Assets.instance.sounds.liveLost);
..
}
private void onCollisionBunnyWithGoldCoin(GoldCoin goldcoin) {
        goldcoin.collected = true;
        AudioManager.instance.play(Assets.instance.sounds.pickupCoin);
        score += goldcoin.getScore();
        Gdx.app.log(TAG, "Gold coin collected");
    }     private void onCollisionBunnyWithFeather(Feather feather) {
        feather.collected = true;
        AudioManager.instance.play(Assets.instance.sounds.pickupFeather);
        score += feather.getScore();
        level.bunnyHead.setFeatherPowerup(true);
        Gdx.app.log(TAG, "Feather collected");
    }

修改BunnyHead:

public void setJumping(boolean jumpKeyPressed) {
switch (jumpState) {
case GROUNDED: // Character is standing on a platform
if (jumpKeyPressed) {
AudioManager.instance.play(Assets.instance.sounds.jump);
// Start counting jump time from the beginning
timeJumping = 0;
jumpState = JUMP_STATE.JUMP_RISING;
}
break;
case JUMP_RISING: // Rising in the air
if (!jumpKeyPressed) {
jumpState = JUMP_STATE.JUMP_FALLING;
}
break;
case FALLING:// Falling down
case JUMP_FALLING: // Falling down after jump
if (jumpKeyPressed && hasFeatherPowerup) {
AudioManager.instance.play(
Assets.instance.sounds.jumpWithFeather, 1,
MathUtils.random(1.0f, 1.1f));
timeJumping = JUMP_TIME_OFFSET_FLYING;
jumpState = JUMP_STATE.JUMP_RISING;
}
break;
}
}

ok,运行起来看看。游戏从无声世界进入了有声世界里。从此,程序员也可以自己玩音乐了。

[libgdx游戏开发教程]使用Libgdx进行游戏开发(10)-音乐和音效的更多相关文章

  1. [libGDX游戏开发教程]使用libGDX进行游戏开发(12)-Action动画

    前文章节列表:  使用libGDX进行游戏开发(11)-高级编程技巧   使用libGDX进行游戏开发(10)-音乐音效不求人,程序员也可以DIY   使用libGDX进行游戏开发(9)-场景过渡   ...

  2. [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计

    声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...

  3. [libgdx游戏开发教程]使用Libgdx进行游戏开发(9)-场景过渡

    本章主要讲解场景过渡效果的使用.这里将用到Render to Texture(RTT)技术. Libgdx提供了一个类,实现了各种常见的插值算法,不仅适合过渡效果,也适合任意特定行为. 在本游戏里面, ...

  4. 使用Html5+C#+微信 开发移动端游戏详细教程: (四)游戏中层的概念与设计

    众所周知,网站的前端页面结构一般是由div组成,父div包涵子div,子div包涵各种标签和项, 同理,游戏中我们也将若干游戏模块拆分成层,在后续的代码维护和游戏程序逻辑中将更加清晰和便于控制. We ...

  5. 微信小程序开发教程 #043 - 在小程序开发中使用 npm

    本文介绍了如何在微信小程序开发中使用 npm 中包的功能,大大提高微信小程序的开发效率,同时也是微信小程序系列教程的视频版更新. 微信小程序在发布之初没有对 npm 的支持功能,这也是目前很多前端开发 ...

  6. PythonWeb开发教程(一),开发之前需要准备什么

    什么是web开发呢,其实就是开发一个网站了.那开发网站需要用到哪些知识呢 1.python基础,因为用python开发的,所以python指定要会,最起码你也得会条件判断,循环,函数,类这些知识: 2 ...

  7. [libgdx游戏开发教程]使用Libgdx进行游戏开发(11)-高级编程技巧 Box2d和Shader

    高级编程技巧只是相对的,其实主要是讲物理模拟和着色器程序的使用. 本章主要讲解利用Box2D并用它来实现萝卜雨,然后是使用单色着色器shader让画面呈现单色状态:http://files.cnblo ...

  8. [libgdx游戏开发教程]使用Libgdx进行游戏开发(2)-游戏框架搭建

    让我们抛开理论开始code吧. 入口类CanyonBunnyMain的代码: package com.packtpub.libgdx.canyonbunny; import com.badlogic. ...

  9. [libgdx游戏开发教程]使用Libgdx进行游戏开发(7)-屏幕布局的最佳实践

    管理多个屏幕 我们的菜单屏有2个按钮,一个play一个option.option里就是一些开关的设置,比如音乐音效等.这些设置将会保存到Preferences中. 多屏幕切换是游戏的基本机制,Libg ...

随机推荐

  1. TCP的挥手协议和握手协议2

    三次握手协议:三次握手协议的主要过程是交互彼此之间的初始序列号,如果没有确认的ACK帧可以么?肯定是可以的 client A -------> server B client A 发送了自己的初 ...

  2. group by 分组后 返回的是一个同属性的集合

    group by 分组后 返回的是一个同属性的集合  我们可以遍历该集合

  3. 【bzoj3653】谈笑风生 DFS序+树状数组

    题目描述 给出一棵以1为根的有根树,q次询问,每次询问给出a和k,求点对 (b,c) 的数目,满足:a.b.c互不相同,b与a距离不超过k,且a和b都是c的祖先. 输入 输入文件的第一行含有两个正整数 ...

  4. BZOJ1857 [Scoi2010]传送带 【三分法】

    题目链接 BZOJ1857 题解 画画图就发现实际上是在\(AB\)上和\(CD\)上分别选两个点\(E\),\(F\),使得\(t_{AE} + t_{EF} + t_{FD}\)最小 然后猜想到当 ...

  5. CF840C On the Bench 解题报告

    CF840C On the Bench 题意翻译 给定\(n\) \((1≤n≤300)\) 个数,求问有多少种排列方案使得任意两个相邻的数之积都不是完全平方数.由于方案数可能很大,输出方案数 \(m ...

  6. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  7. POJ2912:Rochambeau(带权并查集)

    Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5208   Accepted: 1778 题目链接:h ...

  8. poj 2378 Tree Cutting 树形dp

    After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...

  9. zigbee ---- profile 和 cluster

    在zigbee规范中,引入了profile, cluster的概念.具体说来,假设规范一个profile(可以理解成一套规定),这个profile用来规范智能家居领域的相关产品都要满足那些要求,那么h ...

  10. Sed basic and practice

    定义:Sed 是针对数据流的非交谈式编辑器,它在命令行下输入编辑命令并指定文件,然后可以在屏幕上看到编辑命令的输出结果. 好处:Sed 在缓冲区内默认逐行处理数据,所以源文件不会被更改和破坏. 格式: ...