zoukankan      html  css  js  c++  java
  • Libgdx之Music Sound 音效

    教程总文件夹: http://blog.csdn.net/zqiang_55/article/details/50878524

    一个好的游戏。假设没有游戏音效那么这个游戏就过于简单乏味。Libgdx中经常使用的游戏音效有2中,一种是Music。这样的音频文件以流媒体的方式载入,一般文件比較大。另一种是Audio。这样的音频文件一般比較小,直接载入在内存中。主要用于枪声、起跳的声音。

    Libgdx支持ogg, mp3, wav文件格式,官方推荐ogg。 可是个人感觉不是必需非得去转换为ogg,那个文件格式小就用那个。Note:1. RoboVM (iOS) 不支持 OGG 文件类型。 2. 在Android设备上Sound文件不要超过1M。假设超过请用Music接口。

    Audio接口

    Audio接口是用来生成Music和Sound文件的。主要用来管理各种音频文件,底层是在backends里面实现的。
    这里写图片描写叙述

    • 全部用Audio生成的对象,最后都须要调用dispose来销毁
    • Music 对象自己主动调用pause方法当ApplicationListener调用pause方法时。自己主动调用resume

    Music接口

    这里写图片描写叙述
    从上面类图中能够看出Music中经常使用的方法。并且还设置了音乐播放完毕之后的监听函数

    public void setPan (float pan, float volume); 这种方法时设置声音的左右声道,pan=-1是左声道。pan=1是右声道。pan=0声音中间。volume取值在[0,1]。 这种方法能够用在声音由远及近。

    Sound接口

    这里写图片描写叙述

    这里提几个要注意的地方。1. 从上面类图能够看出play方法返回一个long值,这与Music类不同,主要是这个long值代表了当前正在播放的sound的一个值,由于有时候Sound对象可能须要播放多次,这时你想让某一个正在播放的sound对象进行详细操作就能够用到这个long值。2. setPitch方法。这种方法主要用来设置游戏音调,比方说游戏引擎的声音。

    long id = sound.play(1.0f); // play new sound and keep handle for further manipulation
    sound.stop(id);             // stops the sound instance immediately
    sound.setPitch(id, 2);      // increases the pitch to 2x the original pitch
    
    id = sound.play(1.0f);      // plays the sound a second time, this is treated as a different instance
    sound.setPan(id, -1, 1);    // sets the pan of the sound to the left side at full volume
    sound.setLooping(id, true); // keeps the sound looping
    sound.stop(id);             // stops the looping sound 

    測试代码,測试代码包含測试音效的左右声道

        private static final String TAG = AudioTest.class.getSimpleName();
        Music music;
        Sound sound;
    
        Stage stage;
        BitmapFont font;
        Label labelMusic, labelSound, soundLeft, soundRight;
        long soundId;
    
        @Override
        public void create() {
            music = Gdx.audio.newMusic(Gdx.files.internal("sound/song_1.mp3"));
            music.setOnCompletionListener(new OnCompletionListener() {
    
                @Override
                public void onCompletion(Music music) {
                    Gdx.app.log(TAG, "music completed");
                }
            });
            sound = Gdx.audio.newSound(Gdx.files.internal("sound/car-engine.wav"));
    
            stage = new Stage();
            Gdx.input.setInputProcessor(stage);
    
            font = new BitmapFont();
            font.getData().setScale(2.0f);
            Label.LabelStyle style = new Label.LabelStyle();
            style.font = font;
    
            labelMusic = new Label("Music Test", style);
            labelMusic.setPosition(20, Gdx.graphics.getHeight() / 2);
            labelMusic.addListener(new ClickListener() {
    
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    music.play();
                    Gdx.app.log(TAG, "play music");
                }
    
            });
    
            labelSound = new Label("Sound Test", style);
            labelSound.setPosition((Gdx.graphics.getWidth() - labelSound.getWidth()) / 2, 20);
            labelSound.addListener(new ClickListener() {
    
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.log(TAG, "play sound");
                    soundId = sound.play();
                    sound.setLooping(soundId, true);
                }
    
            });
            soundLeft = new Label("Left Test", style);
            soundLeft.setPosition(20, 20);
            soundLeft.addListener(new ClickListener() {
    
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.log(TAG, "Left sound");
                    sound.setPan(soundId, -1, 1.0f);
                }
    
            });
            soundRight = new Label("Right Test", style);
            soundRight.setPosition(Gdx.graphics.getWidth() - soundRight.getWidth()  - 50, 20);
            soundRight.addListener(new ClickListener() {
    
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.log(TAG, "Left sound");
                    sound.setPan(soundId, 1, 1.0f);
                }
    
            });
    
            stage.addActor(labelMusic);
            stage.addActor(labelSound);
            stage.addActor(soundLeft);
            stage.addActor(soundRight);
        }
    
        @Override
        public void render() {
            Gdx.gl.glClearColor(0.39f, 0.58f, 0.92f, 1.0f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            stage.act();
            stage.draw();
        }
    
        @Override
        public void dispose() {
            music.dispose();
            font.dispose();
            stage.dispose();
        }

    这里写图片描写叙述

  • 相关阅读:
    正则表达式在线测试(生成)工具
    org.eclipse.swt.custom.StyledText.getScrollbarsMode()I
    MySQL修改表一次添加多个列(字段)和索引
    How can I view currently running MySQL queries?( 查看正在运行的MySQL语句/脚本命令)
    faster alter table add column
    提取data.frame中的部分数据(不含列标题和行标题)
    How to generate a random number in R
    INSTALLMENT of QValue
    Linux 执行ll命令时指定按文件时间或大小排序
    替换 data.frame 中的特殊的值
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7363954.html
Copyright © 2011-2022 走看看