libgdx [Audio]

本文探讨了游戏开发中多媒体技术的应用,包括声音、音乐、图像处理和AR特效的集成,以及如何通过优化技术实现更丰富的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Sound短时声音
  • Music长时声音
package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.Image;

public class MainMenuStage extends Stage {

    Texture texture;
    TextureRegion textureRegion;
    TextureRegion newGameTextureRegion;
    TextureRegion soundRegion;
    TextureRegion musicRegion;
    Image bgImage;
    Image newGameImage;
    Image soundImage;
    Image musicImage;

    Sound sound;
    Music music;

    public MainMenuStage() {
        super();
        Gdx.app.log("MyGdxGame", "MainMenuStage");
        init();
    }

    public void init() {
        texture = new Texture(Gdx.files.internal("data/mainmenu_stage.jpg"));
        textureRegion = new TextureRegion(texture, 0, 48, 256, 208);
        bgImage = new Image(textureRegion);
        bgImage.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        newGameTextureRegion = new TextureRegion(texture, 0, 0, 256, 48);
        newGameImage = new Image(newGameTextureRegion);
        newGameImage.setPosition((Gdx.graphics.getWidth() - 256 * 2) / 2, (Gdx.graphics.getHeight() - 48) / 2);
        newGameImage.setSize(256 * 2, 48 * 2);

        soundRegion = new TextureRegion(texture, 0, 256, 256, 48);
        soundImage = new Image(soundRegion);
        soundImage.setPosition((Gdx.graphics.getWidth() - 256 * 2) / 2, (Gdx.graphics.getHeight() - 48) / 2 - 128);
        soundImage.setSize(256 * 2, 48 * 2);

        musicRegion = new TextureRegion(texture, 0, 304, 256, 48);
        musicImage = new Image(musicRegion);
        musicImage.setPosition((Gdx.graphics.getWidth() - 256 * 2) / 2, (Gdx.graphics.getHeight() - 48) / 2 - 256);
        musicImage.setSize(256 * 2, 48 * 2);

        this.addActor(bgImage);
        this.addActor(newGameImage);
        this.addActor(soundImage);
        this.addActor(musicImage);

        newGameImage.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                                     int pointer, int button) {
                StateStatus.flag = StateStatus.GAME_STAGE;
                return true;
            }
        });

        soundImage.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                                     int pointer, int button) {
                sound.play();
                return true;
            }
        });
        musicImage.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                                     int pointer, int button) {
                if(!music.isPlaying())
                    music.play();
                else
                    music.stop();
                return true;
            }
        });

        sound =  Gdx.audio.newSound((Gdx.files.internal("data/chirp.wav")));
        music = Gdx.audio.newMusic(Gdx.files.internal("data/bubblepop.ogg"));
        music.setLooping(true);
    }

    @Override
    public void dispose() {
        super.dispose();
        Gdx.app.log("MyGdxGame", "MainMenuStage dispose");
        sound.dispose();
        music.dispose();
    }


}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值