libgdx学习记录15——音乐Music播放
背景音乐是游戏中必备的元素,好的背景音乐能为游戏加分不少,使人更容易融入到游戏的氛围中去。
Music类中主要有以下函数:
play()播放
stop()停止
pause()暂停
setVolume()设置音量
setLooping()是否循环播放
代码示例:
package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; public class Lib015_Music extends ApplicationAdapter{ Music music;
Sound sound; Skin skin;
Stage stage;
State state; enum State{ music_play, music_stop, music_pause }; @Override
public void create() {
// TODO Auto-generated method stub
super.create(); music = Gdx.audio.newMusic( Gdx.files.internal( "audio/xjwq.mp3" ) );
music.setLooping( true );
music.setVolume( 0.5f );
//music.play(); stage = new Stage();
skin = new Skin( Gdx.files.internal( "skin/uiskin.json" ) );
final TextButton buttonStop = new TextButton( "Stop", skin );
TextButton buttonPlay = new TextButton( "Play/Pause", skin ); state = State.music_stop;
buttonStop.setDisabled( true );
buttonStop.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y,int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
public void touchUp(InputEvent event, float x, float y,int pointer, int button) {
// TODO Auto-generated method stub
if( state != State.music_stop ){
music.stop();
state = State.music_stop;
buttonStop.setDisabled( true );
System.out.println( "stop" );
}
}
}); buttonPlay.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y,int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
public void touchUp(InputEvent event, float x, float y,int pointer, int button) {
// TODO Auto-generated method stub
if( state == State.music_play ){
music.pause();
state = State.music_pause;
System.out.println( "pause" );
}
else{
music.play();
state = State.music_play;
buttonStop.setDisabled( false );
System.out.println( "play" );
}
//(state==State.music_play)? music.pause(): music.play();
}
}); final Slider slider = new Slider( 0, 100, 1, false, skin );
slider.addListener(new ChangeListener(){
public void changed(ChangeEvent event, Actor actor) {
// TODO Auto-generated method stub
music.setVolume( slider.getValue()/100 );
}
}); slider.setValue( 50 );
Table table = new Table();
table.defaults().space(5); table.row();
table.add( new Label( "Music Play", skin ) ).colspan(2).expandX();
table.row();
table.add( slider ).colspan(2).expandX();
table.row();
table.add( buttonPlay ).minWidth(100);
table.add( buttonStop ).minWidth(100);
table.pad( 10 );
table.pack();
table.setBackground( skin.newDrawable( "white", Color.PINK ) ); stage.addActor( table );
table.setPosition( stage.getWidth()/2-table.getWidth()/2, stage.getHeight()/2-table.getHeight()/2 );
Gdx.input.setInputProcessor( stage );
} @Override
public void render() {
// TODO Auto-generated method stub
super.render(); Gdx.gl.glClearColor( 1, 1, 1, 1 );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw();
} @Override
public void dispose() {
// TODO Auto-generated method stub
music.dispose();
super.dispose();
} }
运行效果:

中间滑动条是调节音量的,左下是播放暂停键,右下是停止键。
另外Sound与Music类似。
libgdx学习记录15——音乐Music播放的更多相关文章
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
- libgdx学习记录20——多线程MultiThread资源处理
在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...
- libgdx学习记录19——图片动态打包PixmapPacker
libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...
- libgdx学习记录17——照相机Camera
照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...
- libgdx学习记录16——资源加载器AssetManager
AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...
随机推荐
- css设计并排布局
css code form#reset_password ul { list-style: none; margin: 0 0 20px 200px; padding:; } form#reset_p ...
- webpack+sass+vue 入门教程(三)
十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...
- zabbix系列之安全
https://blog.csdn.net/xiaoyu_0217/article/details/73500125 存在问题: 1)zabbix的Admin口令太弱或使用默认口令(Admin/zab ...
- VMWare12虚拟机实现主客机间的文件拖拽(复制粘贴)和文件夹共享
版本: 主机:Windows 7 64位旗舰版 虚拟机: VMWare 12 + Windows 7 64位旗舰版 VMWare pro 12 + Ubuntu16.04LTS 64位 注:由于VMW ...
- mysql client之init-command
If the server is a replication master and you want to avoid replicating the content to replication s ...
- pt-osc全解pt-online-schema-change
MySQL 大字段的DDL操作:加减字段.索引.修改字段属性等,在5.1之前都是非常耗时耗力的,特别是会对MySQL服务产生影响.在5.1之后随着Plugin Innodb的出现在线加索引的提高了很多 ...
- 学习笔记:Analyze MySQL Performance及慢日志的开启
Table of Contents Analyze MySQL PerformanceTuningSlow queries and Slowlog Brought to you by Rick Jam ...
- priority_queue的优先级变化(结构体的写法)
priority_queue的优先级变化(结构体的写法) 在头文件中加上#include <queue> 即可使用stl中的库函数priority_queue,优先队列默认的是从大到小的优 ...
- 洛谷P1803
#include <iostream>#include <algorithm>#include <cstdio>using namespace std; struc ...
- stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list
stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...