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. ...
随机推荐
- Python笔记(五):异常处理和数据存储
注:和上一篇有关联 (一) finally 和 输出异常信息 try: the_man = open(r'C:\Users\123456\Desktop\test.txt') ...
- 在 Azure 中的 Windows 虚拟机上使用 SSL 证书保护 IIS Web 服务器
若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Windows ...
- SQL Server 的 RowGuid/RowGuidCol 是什么意思?
SQL Server 中设计表时,有个属性叫 RowGuid(用 SQL 时,关键词是 RowGuidCol),这个是什么意思呢?这个是全局唯一标识符,与“标识”不同,标识是只在本表中唯一,全局唯一标 ...
- 详解JNDI的lookup资源引用java:/comp/env
ENC的概念: The application component environment is referred to as the ENC, the enterprise naming c ...
- 中式台球 规则 ( ChinaBilliards )
中式台球比赛规则 中式台球兴起于上世纪八十年代末,之前叫法有“中式8球”.“中式9球”.“十六彩”.“美式落袋”.“普尔“.”八球””等等.中国台球协会于2012年宣布统一该项运动称呼,定名为“中式台 ...
- rename 批量修改文件名简单用法
有的时候我们需要批量创建文件做测试,为了做区分,一般只要稍稍动动文件名即可,MV命令既可以移动文件,也是可以修改文件名的,但批量修改文件名MV做不到,此时,我们可以用rename命令批量修改是蛮不错的 ...
- python下以api形式调用tesseract识别图片验证码
一.背景 之前在博文中介绍在python中如何调用tesseract ocr引擎,当时主要介绍了shell模式,shell模式需要安装tesseract程序,并且效率相对略低. 今天介绍api形式的调 ...
- VPC见解
VPC是什么? VPC:Virtual Private Cloud,即虚拟私有云.讨论VPC时,我们可以从两个方面来讨论: 从服务的角度来看:VPC是一种云,但是这个云不属于我们常见的公有云.私有 ...
- 阿里八八Alpha阶段Scrum(2/12)
今日进度 叶文滔: 11.1:搭建Andriod Studio开发环境 11.2:已经完成Alpha阶段的APP整体框架搭建. 11.3:根据会议讨论内容,增加了模块标题栏返回键. 王国超: 完成了多 ...
- Django商城项目笔记No.8用户部分-注册接口实现
Django商城项目笔记No.8用户部分-注册接口实现 users的view.py中增加如下代码 class RegisterUserView(CreateAPIView): "" ...