教程总文件夹: 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. SQL Server复制故障(1)

    初始化时报错 错误消息: The process could not read file '\\176WINDOWSSQL\ReplData\unc\176WINDOWSSQL_DB2_TEST3\2 ...

  2. cocos2d-js 免安装在线版 粒子编辑器 particle editor particle builder 兼容pex和plist

    http://onebyonedesign.com/flash/particleeditor/ 这个原来是为flash starling设计的粒子系统编辑器,但实际上,还是能兼容cocos2d的. 只 ...

  3. django之创建第8个项目-数据库配置及同步研究

    1.sqlitestudio-2.1.5数据库可视化工具--百度云盘下载 2.编写C:\djangoweb\helloworld\blog\models.py文件 # Create your mode ...

  4. RHEL7 DNS 服务 unbound 测试

    测试环境: 物理机win10系统,虚拟机软件使用Oracle VirtualBox. rhel1.rusky.com 192.168.100.1 RHEL7(辅DNS) rhel2.rusky.com ...

  5. Windows在cmd杀掉进程

    问题描述: 在windows根据pid杀进程 问题解决: tasklist查看当前系统中的进程列表,然后针对你要杀的进程使用taskkill命令 #根据服务名taskkill /im nginx.ex ...

  6. onActivityResult 传递数据

    onActivityResult 传递数据 http://www.cnblogs.com/sipher/articles/2435078.html 如下图所示.当菜单项变多时,出现了垂直的滚动条,选项 ...

  7. Linux-静态库生成

    Linux上的静态库,其实是目标文件的归档文件.在Linux上创建静态库的步骤如下: 写源文件,通过 gcc -c xxx.c 生成目标文件. 用 ar 归档目标文件,生成静态库. 配合静态库,写一个 ...

  8. python中对两个 list 求交集,并集和差集

    python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3 ...

  9. RFID-RC522 与Arduino的连接

    一.前几天在某宝上刚买了个RFID-RC522  ,目标是复制我的门禁卡(看样子没多大希望了).二.各种百度各种谷歌都没找到与Arduino的连接方式. so,分享下我的连接方式,与大家共同进步... ...

  10. 【MongoDB】MongoDB的安装教程

    1,MongoDB简介 MongoDB也是一种数据库,只不过它既不是关系型数据库,也不是非关系型数据库(NoSQL),而是一种介于关系型数据库和NoSQL之间的一种数据库.如果说非关系型数据库是轻量级 ...