背景音乐是游戏中必备的元素,好的背景音乐能为游戏加分不少,使人更容易融入到游戏的氛围中去。

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播放的更多相关文章

  1. libgdx学习记录18——Box2d物理引擎

    libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...

  2. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  3. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  4. libgdx学习记录26——Polygon多边形碰撞检测

    libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...

  5. libgdx学习记录22——3d物体创建

    libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...

  6. libgdx学习记录20——多线程MultiThread资源处理

    在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...

  7. libgdx学习记录19——图片动态打包PixmapPacker

    libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...

  8. libgdx学习记录17——照相机Camera

    照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...

  9. libgdx学习记录16——资源加载器AssetManager

    AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...

随机推荐

  1. Charles抓取https请求

    最近公司将Windows产品的http请求,替换成https请求了,当https请求超过5次失败,就自动切换回http请求.测试时使用Charles抓包测试. 一.http抓包 http抓包比较简单, ...

  2. CSS 小结笔记之解决flex布局边框对不齐

    在使用flex 进行伸缩布局的时候,经常会给子盒子设置边框,这时经常会出现上下边框对不齐的情况.本篇文章来探讨并解决这个问题. 具体出现的问题如下图所示 具体代码如下 <!DOCTYPE htm ...

  3. Ubuntu 18.04 修改为静态IP

    1.进入/etc/netplan目录 cd /etc/netplan 2.查看文件 ls 3.编辑 01-network-manager-all.yaml vim 01-network-manager ...

  4. ubuntu下给raspy pi2 编译qt5库

    交叉编译时,通常要准备mkspec, 参考: http://wiki.qt.io/Building_Qt_for_Embedded_Linux mkspec包含两个文件: qmake.conf- Th ...

  5. tomcat catalina.out日志切割(logrotate)

    简单说明: 1,因为tomcat日志会一直往catalina.out里面输出,所以回到值catalina.out非常大,占用磁盘空间 2,日志非常大,查看日志就需要很长时间. 3,据说catalina ...

  6. textbox只允许输入数字

    private void txtUserId_KeyPress(object sender, KeyPressEventArgs e) { //如果输入的不是数字键,也不是回车键.Backspace键 ...

  7. python之demo1----改编自turtle.py文件中的demo

    """ 改编自turtle.py自带demo 执行 python -m turtledemo 命令查看系统内置demo的源码 绘制:需要通过import turtle引入 ...

  8. 关于MSCOMM.OCX无法正常注册的问题解决

    [问题] 关于“Component'MSCOMM32.OCX'or one of its dependencies not correctly registered: afole is missing ...

  9. tail 尾巴

    tail用法:尾巴,取文件的最后N行,默认前10行, -n 2 取前2行-n 2,简写就是-2 -f 文件 跟踪一个文件尾部的时时变化. 克隆出一个窗口执行:循环脚本:for n in `seq 1 ...

  10. November 10th, 2017 Week 45th Friday

    A little bit of mercy makes the world less cold and more just. 多一点怜悯就可以让这个世界少一点冷酷而多一点正义. Maybe there ...