zoukankan      html  css  js  c++  java
  • libgdx学习记录15——音乐Music播放

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

    Music类中主要有以下函数:

    play()播放

    stop()停止

    pause()暂停

    setVolume()设置音量

    setLooping()是否循环播放

    代码示例:

      1 package com.fxb.newtest;
      2 
      3 import com.badlogic.gdx.ApplicationAdapter;
      4 import com.badlogic.gdx.Gdx;
      5 import com.badlogic.gdx.audio.Music;
      6 import com.badlogic.gdx.audio.Sound;
      7 import com.badlogic.gdx.graphics.Color;
      8 import com.badlogic.gdx.graphics.GL10;
      9 import com.badlogic.gdx.scenes.scene2d.Actor;
     10 import com.badlogic.gdx.scenes.scene2d.InputEvent;
     11 import com.badlogic.gdx.scenes.scene2d.InputListener;
     12 import com.badlogic.gdx.scenes.scene2d.Stage;
     13 import com.badlogic.gdx.scenes.scene2d.ui.Label;
     14 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
     15 import com.badlogic.gdx.scenes.scene2d.ui.Slider;
     16 import com.badlogic.gdx.scenes.scene2d.ui.Table;
     17 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
     18 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
     19 
     20 public class Lib015_Music extends ApplicationAdapter{
     21     
     22     Music music;
     23     Sound sound;
     24     
     25     Skin skin;
     26     Stage stage;
     27     State state;
     28     
     29     enum State{ music_play, music_stop, music_pause };
     30     
     31     @Override
     32     public void create() {
     33         // TODO Auto-generated method stub
     34         super.create();
     35         
     36         music = Gdx.audio.newMusic( Gdx.files.internal( "audio/xjwq.mp3" ) );
     37         music.setLooping( true );
     38         music.setVolume( 0.5f );
     39         //music.play();
     40     
     41         stage = new Stage();
     42         skin = new Skin( Gdx.files.internal( "skin/uiskin.json" ) );
     43         final TextButton buttonStop = new TextButton( "Stop", skin );
     44         TextButton buttonPlay = new TextButton( "Play/Pause", skin );
     45         
     46         state = State.music_stop;
     47         buttonStop.setDisabled( true );
     48         buttonStop.addListener(new InputListener(){
     49             public boolean touchDown(InputEvent event, float x, float y,int pointer, int button) {
     50                 // TODO Auto-generated method stub
     51                 return true;
     52             }
     53             public void touchUp(InputEvent event, float x, float y,int pointer, int button) {
     54                 // TODO Auto-generated method stub
     55                 if( state != State.music_stop ){
     56                     music.stop();
     57                     state = State.music_stop;
     58                     buttonStop.setDisabled( true );
     59                     System.out.println( "stop" );
     60                 }
     61             }            
     62         });
     63         
     64         buttonPlay.addListener(new InputListener(){
     65             public boolean touchDown(InputEvent event, float x, float y,int pointer, int button) {
     66                 // TODO Auto-generated method stub
     67                 return true;
     68             }
     69             public void touchUp(InputEvent event, float x, float y,int pointer, int button) {
     70                 // TODO Auto-generated method stub
     71                 if( state == State.music_play ){
     72                     music.pause();
     73                     state = State.music_pause;
     74                     System.out.println( "pause" );
     75                 }
     76                 else{
     77                     music.play();
     78                     state = State.music_play;
     79                     buttonStop.setDisabled( false );    
     80                     System.out.println( "play" );
     81                 }                            
     82                 //(state==State.music_play)? music.pause(): music.play();
     83             }            
     84         });    
     85         
     86             
     87         final Slider slider = new Slider( 0, 100, 1, false, skin );
     88         slider.addListener(new ChangeListener(){
     89             public void changed(ChangeEvent event, Actor actor) {
     90                 // TODO Auto-generated method stub
     91                 music.setVolume( slider.getValue()/100 );
     92             }        
     93         });
     94         
     95         slider.setValue( 50 );
     96         Table table = new Table();
     97         table.defaults().space(5);
     98 
     99         table.row();
    100         table.add( new Label( "Music Play", skin )  ).colspan(2).expandX();
    101         table.row();
    102         table.add( slider ).colspan(2).expandX();
    103         table.row();
    104         table.add( buttonPlay ).minWidth(100);
    105         table.add( buttonStop ).minWidth(100);
    106         table.pad( 10 );
    107         table.pack();
    108         table.setBackground( skin.newDrawable( "white", Color.PINK ) );
    109 
    110         stage.addActor( table );
    111         table.setPosition( stage.getWidth()/2-table.getWidth()/2, stage.getHeight()/2-table.getHeight()/2 );
    112         Gdx.input.setInputProcessor( stage );
    113     }
    114 
    115     @Override
    116     public void render() {
    117         // TODO Auto-generated method stub
    118         super.render();
    119         
    120         Gdx.gl.glClearColor( 1, 1, 1, 1 );
    121         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
    122         
    123         stage.act();
    124         stage.draw();
    125     }
    126 
    127     @Override
    128     public void dispose() {
    129         // TODO Auto-generated method stub
    130         music.dispose();
    131         super.dispose();
    132     }
    133 
    134 }

    运行效果:

    中间滑动条是调节音量的,左下是播放暂停键,右下是停止键。

    另外Sound与Music类似。

  • 相关阅读:
    如何选出一支优秀的债券基金?
    来吧,今天说说股票型基金
    来吧,帮你认识基金
    债券基金有A、有B、还有C,到底买哪种?
    基金到底分几类
    来吧,一个指标让你选到优秀的“固收+"基金
    来吧,带你读懂债券基金
    Windows 彻底删除文件
    IntelliJ IDEA 工程、模块常用操作
    IntelliJ IDEA 创建项目(开发第一个程序)
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/3773212.html
Copyright © 2011-2022 走看看