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快速搭建本地网站
如果你急需一个简单的Web Server,但你又不想去下载并安装那些复杂的HTTP服务程序,比如:Apache,ISS,Nodejs等.那么, Python 可能帮助你.使用Python可以完成一个简 ...
- windows环境下 nginx+iis 反向代理解决跨域问题
项目基本完成,是时候花点时间整理一下最近的姿势了 1 什么是跨域? 网上对于跨域的概念会有大篇幅的文章去解释,似乎有点玄乎,初学者很容易对这个概念产生恐惧,跨域其实很简单,其实只要知道一点,无法跨域访 ...
- tomcat8.5 Host-Manager配置访问的方法
1. 安装配置tomcat服务器,浏览器输入 localhost:8080,可正常访问主页,但访问localhost:8080/host-manager, localhost:8080/manager ...
- mysql 大文件导入导出
导出:mysqldump -u用户名 -p密码 -hIP地址 数据库名 > /dump.sql示例:mysqldump -uroot -proot -h127.0.0.1 test > / ...
- 如何删除sharepoint列表List中的全部数据。
可以使用excel,但是powershell会比较方便 (admin mode - Sharepoint powershell) [System.reflection.Assembly]::LoadW ...
- python基本语法:
http://www.runoob.com/python/python-basic-syntax.html
- python中判断实例可迭代地几种方式
1. 利用 __iter__内建属性 if hasattr(obj, '__iter__') : print 'iterable' 这种方法不能检测字符串,如:hasattr('', '__iter_ ...
- 网络唤醒(WOL)全解指南:原理篇
什么是网络唤醒 网络唤醒(Wake-on-LAN,WOL)是一种计算机局域网唤醒技术,使局域网内处于关机或休眠状态的计算机,将状态转换成引导(Boot Loader)或运行状态.无线唤醒(Wake-o ...
- 028、HTML 标签3表单标签插入组件
内容:表单标签插入组件(经常使用)############################################################## form表单标签和input组件 < ...
- PyQt5--ShowTips
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...