教程总文件夹: http://blog.csdn.net/zqiang_55/article/details/50878524

一个好的游戏。假设没有游戏音效那么这个游戏就过于简单乏味。Libgdx中经常使用的游戏音效有2中,一种是Music。这样的音频文件以流媒体的方式载入,一般文件比較大。另一种是Audio。这样的音频文件一般比較小,直接载入在内存中。主要用于枪声、起跳的声音。

Libgdx支持ogg, mp3, wav文件格式,官方推荐ogg。 可是个人感觉不是必需非得去转换为ogg,那个文件格式小就用那个。Note:1. RoboVM (iOS) 不支持 OGG 文件类型。 2. 在Android设备上Sound文件不要超过1M。假设超过请用Music接口。

Audio接口

Audio接口是用来生成Music和Sound文件的。主要用来管理各种音频文件,底层是在backends里面实现的。

  • 全部用Audio生成的对象,最后都须要调用dispose来销毁
  • Music 对象自己主动调用pause方法当ApplicationListener调用pause方法时。自己主动调用resume

Music接口



从上面类图中能够看出Music中经常使用的方法。并且还设置了音乐播放完毕之后的监听函数

public void setPan (float pan, float volume); 这种方法时设置声音的左右声道,pan=-1是左声道。pan=1是右声道。pan=0声音中间。volume取值在[0,1]。 这种方法能够用在声音由远及近。

Sound接口

这里提几个要注意的地方。1. 从上面类图能够看出play方法返回一个long值,这与Music类不同,主要是这个long值代表了当前正在播放的sound的一个值,由于有时候Sound对象可能须要播放多次,这时你想让某一个正在播放的sound对象进行详细操作就能够用到这个long值。2. setPitch方法。这种方法主要用来设置游戏音调,比方说游戏引擎的声音。

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 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

測试代码,測试代码包含測试音效的左右声道

    private static final String TAG = AudioTest.class.getSimpleName();
Music music;
Sound sound; Stage stage;
BitmapFont font;
Label labelMusic, labelSound, soundLeft, soundRight;
long soundId; @Override
public void create() {
music = Gdx.audio.newMusic(Gdx.files.internal("sound/song_1.mp3"));
music.setOnCompletionListener(new OnCompletionListener() { @Override
public void onCompletion(Music music) {
Gdx.app.log(TAG, "music completed");
}
});
sound = Gdx.audio.newSound(Gdx.files.internal("sound/car-engine.wav")); stage = new Stage();
Gdx.input.setInputProcessor(stage); font = new BitmapFont();
font.getData().setScale(2.0f);
Label.LabelStyle style = new Label.LabelStyle();
style.font = font; labelMusic = new Label("Music Test", style);
labelMusic.setPosition(20, Gdx.graphics.getHeight() / 2);
labelMusic.addListener(new ClickListener() { @Override
public void clicked(InputEvent event, float x, float y) {
music.play();
Gdx.app.log(TAG, "play music");
} }); labelSound = new Label("Sound Test", style);
labelSound.setPosition((Gdx.graphics.getWidth() - labelSound.getWidth()) / 2, 20);
labelSound.addListener(new ClickListener() { @Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log(TAG, "play sound");
soundId = sound.play();
sound.setLooping(soundId, true);
} });
soundLeft = new Label("Left Test", style);
soundLeft.setPosition(20, 20);
soundLeft.addListener(new ClickListener() { @Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log(TAG, "Left sound");
sound.setPan(soundId, -1, 1.0f);
} });
soundRight = new Label("Right Test", style);
soundRight.setPosition(Gdx.graphics.getWidth() - soundRight.getWidth() - 50, 20);
soundRight.addListener(new ClickListener() { @Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log(TAG, "Left sound");
sound.setPan(soundId, 1, 1.0f);
} }); stage.addActor(labelMusic);
stage.addActor(labelSound);
stage.addActor(soundLeft);
stage.addActor(soundRight);
} @Override
public void render() {
Gdx.gl.glClearColor(0.39f, 0.58f, 0.92f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act();
stage.draw();
} @Override
public void dispose() {
music.dispose();
font.dispose();
stage.dispose();
}

Libgdx之Music Sound 音效的更多相关文章

  1. 11.1、Libgdx的音频之音效

    (官网:www.libgdx.cn) 音效通常是比较小的音频文件,通常是几秒钟的长度.通常用在特定的游戏事件中,比如跳跃或者射击. 音效可以保存为多种格式.Libgdx支持MP3.OGG和WAV文件. ...

  2. Libgdx教程目录

    Libgdx教程 Note:本教程用的Libgdx 1.9.2版本,在教程更新完毕之前应该不会更新版本.之前的博客中也发表过Libgdx的内容,不过当时都从别人那里拷贝的,因此现在想做一个Libgdx ...

  3. 3D音效

    摘自:http://baike.baidu.com/view/1330437.htm?fr=aladdin 3D音效就是用扬声器仿造出似乎存在但是虚构的声音.例如扬声器仿造头顶上有一架飞机从左至右飞过 ...

  4. 零基础学习云计算及大数据DBA集群架构师【Linux系统配置及网络配置2015年12月30日周三】

    /Mon *************摘要************** 计划任务 )一次性计划任务 服务:atd 命令:at 服务存放文件:/etc/init.d/atd 系统配置文件:/etc/at. ...

  5. 浅析Linux启动流程

    Linux系统启动流程 Linux 系统的启动,从计算机开机通电自检开始,一直到登陆系统,需要经历多个过程.了解 Linux 的启动过程,有助于了解 Linux 系统的结构,也对系统的排错有很大的帮助 ...

  6. [libgdx游戏开发教程]使用Libgdx进行游戏开发(10)-音乐和音效

    本章音效文件都来自于公共许可: http://files.cnblogs.com/mignet/sounds.zip 在游戏中,播放背景音乐和音效是基本的功能. Libgdx提供了跨平台的声音播放功能 ...

  7. 11.4、Libgdx的音频之录制PCM音效

    (官网:www.libgdx.cn) 可以通过AudioRecorder接口访问PCM数据.通过如下方式创建一个接口实例: AudioRecorder recorder = Gdx.audio.new ...

  8. 使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()

    1.适用范围:一些很小的提示或警告音频. 2.使用限制: 声音长度不能超过30秒 声音文件必须是PCM或IMA4(IMA/ADPCM)格式.(有时候可播放一些特殊的.mp3) 打包成.caf..aif ...

  9. 5、使用Libgdx设计一个简单的游戏------雨滴

    (原文:http://www.libgdx.cn/topic/49/5-%E4%BD%BF%E7%94%A8libgdx%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E7% ...

随机推荐

  1. Ant脚本简介与基础知识

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6624003.html  一:Ant是什么 Ant相当于Linux环境下的shell脚本,只不过是用xml文档来 ...

  2. 〖Linux〗tmux 配置文件

    tmux中默认的shell是zsh,zsh在日常中使用可以协助我们高效地使用shell命令. 配置文件默认位置 ~/.tmux.conf,此外,把常用快捷键也附录在这里,方便记忆. # 设置Shell ...

  3. V-rep学习笔记:机器人模型创建2—添加关节

    下面接着之前经过简化并调整好视觉效果的模型继续工作流,为了使模型能受控制运动起来必须在合适的位置上添加相应的运动副/关节.一般情况下我们可以查阅手册或根据设计图纸获得这些关节的准确位置和姿态,知道这些 ...

  4. DirectX中文手册

    目  录 第一章 DirectX基础(初级篇) 第一节  什么是DirectX 一.什么是DirectX ? 二.DirectX的组成部分 三.关于DirectDraw 四.为什么要使用DirectD ...

  5. [转载]ubuntu防火墙设置

    原文地址:ubuntu防火墙设置作者:風飏    自打2.4版本以后的Linux内核中, 提供了一个非常优秀的防火墙工具.这个工具可以对出入服务的网络数据进行分割.过滤.转发等等细微的控制,进而实现诸 ...

  6. Swift3 - compare方法之ComparisonResult说明

    Swift3在实现两个对象比较时,引入了compare方法,其中,方法返回值ComparisonResult解释如下: ComparisonResult是一个枚举类型,包含了以下3个成员: 其中: q ...

  7. ILP32、ILP64、LP64、LLP64、64位系统

    Data Type     ILP32      ILP64     LP64      LLP64char              8             8            8     ...

  8. hihocoder第218周:AC自动机

    题目链接 问题描述 给定n个单词,给定一个长字符串s,单词总长度和字符串s的长度都不超过1e5.要求把s中所有的出现单词的位置用*替代. 例如: 样例输入 2 abc cd abcxyzabcd 样例 ...

  9. JavaScript Math Object 数字

    JavaScript Math Object Math Object The Math object allows you to perform mathematical tasks. Math is ...

  10. ReactiveCocoa 5.0 初次了解

    RAC 5.0 相比于 4.0 有了巨大的变化,不仅是受 swift 3.0 大升级的影响,RAC 对自身项目结构的也进行了大幅度的调整.这个调整就是将 RAC 拆分为四个库:ReactiveCoco ...