播放音乐时,Libgdx会发出噪音

时间:2015-02-20 11:39:34

标签: java android audio libgdx

我有这个问题,当我用libgdx播放这个音乐时,我开始在播放音乐的同时在背景中听到一些柔和的砰砰声。我尝试了一些不同的音频格式,但它没有帮助。在我的代码中,音乐是" endmusic"。

以下是我的全部代码:

public class WorldScreen implements Screen{

Preferences prefs;

private Texture bgCity;
private Texture bgLoop;
private Texture bgEnd;
private Texture bgFade;
private Texture hud;

public static OrthographicCamera camera;
SpriteBatch batch;

Rectangle player;
Rectangle background;
Rectangle backgroundloop;

public float time = 100;
public float camx = 0;
public float camy = 0;
public static float score = 384400000f;
public static float value = 384400000f;
BitmapFont font;
GestureListenerC controller;

private Music startmusic;
private static Music loopmusic;
private Music endMusic;

public static boolean ending = false;

private int endint;
private String endstring = "";

public WorldScreen(final JetpackGame aa) {

}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camx = camera.viewportWidth / 2f;
    camy = camera.viewportHeight / 2f;

    controller.update();
    camera.update();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    batch.draw(bgCity, background.x, background.y);
    batch.draw(bgLoop, backgroundloop.x, backgroundloop.y);
    batch.draw(bgEnd, 0, 5360);

     if(!ending){
    if(camera.position.y >= 5360) camera.position.y = 4096;
     }

    if(value <= 0) ending = true;
    //ENDING
    if(ending){
        time += 0.1f * Gdx.graphics.getDeltaTime();
        startmusic.stop();
        loopmusic.stop();
        if(time <= 102.75f){
            camera.position.y += 0.1 * time;
            if(camera.position.y >= 5360) camera.position.y = 4096;
        }
        if(time >= 102.75f) {
            if (camera.position.y >= 4090 && camera.position.y <= 6666)
                camera.position.y += 0.1 * time;
        }
        endMusic.play();
        font.setScale(2.0f);
        font.draw(batch, "You already finished the game xD", 0,1200);
        if(time >= 103.2f)endMusic.stop();

    }


    Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.setProjectionMatrix(normalProjection);

    batch.draw(hud, 0, 85, Gdx.graphics.getWidth(), 1200);

    font.setScale(3.5f);
    if(time <=102.5f) {
        font.draw(batch, ">Meters to the moon", 10, 1260);
        font.draw(batch, ">" + (int)value + "m", 10, 1190);
    }
    //font.draw(batch,""+time, 100, 100);
    //font.draw(batch,""+value, 100,120);
    if(time >= 103) {
        font.setScale(3.0f);
        font.draw(batch, ">"+endstring, 10,1200);
    }

    if(time >= 104)
    batch.draw(bgFade, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    batch.end();

    if(value < 0) value = 0;

    if(!startmusic.isPlaying()) {
        loopmusic.setLooping(true);
        loopmusic.play();
    }

    //RANDOM ENDING GENERATOR
    switch(endint){
        case 1: endstring = "Now do it again faster!";
            break;
        case 2: endstring = "What are you doing with your life?!";
            break;
        case 3: endstring = "Do you want a cookie?";
            break;
        case 4: endstring = "How many hours did you waste?";
            break;
        case 5: endstring = "So you have no life?";
            break;
        case 6: endstring = "Now get out of your room and go outside!";
            break;
      }

}
@Override
public void resize(int width, int height) {

}

@Override
public void show() {

    Random rand = new Random();
    endint = rand.nextInt(6);

    font = new BitmapFont();
    font.setColor(Color.GREEN);

    Preferences prefs = Gdx.app.getPreferences("My Preferences");
    float value = prefs.getFloat("Value", 384400000f);

    controller = new GestureListenerC();
    GestureDetector gestureDetector = new GestureDetector(20, 0.5f, 2, 0.15f, controller);
    Gdx.input.setInputProcessor(gestureDetector);

    loopmusic = Gdx.audio.newMusic(Gdx.files.internal("audio/AmbientLoop.ogg"));
    startmusic = Gdx.audio.newMusic(Gdx.files.internal("audio/AmbientStart.ogg"));
    endMusic = Gdx.audio.newMusic(Gdx.files.internal("audio/Moon.ogg"));
    endMusic.setLooping(false);
    startmusic.play();

    bgCity = new Texture(Gdx.files.internal("img/city_BG.png"));
    bgLoop = new Texture(Gdx.files.internal("img/loopBG.png"));
    bgEnd = new Texture(Gdx.files.internal("img/endBG.png"));
    bgFade = new Texture(Gdx.files.internal("img/fade.png"));
    hud = new Texture(Gdx.files.internal("img/Hud.png"));

    camera = new OrthographicCamera();
    camera.setToOrtho(false, 480,854);
    batch = new SpriteBatch();

    background = new Rectangle();
    background.x = 0;
    background.y = 0;
    background.width = 479;
    background.height = 4096;

    backgroundloop = new Rectangle();
    backgroundloop.x = 0;
    backgroundloop.y = 4096;
    backgroundloop.width = 512;
    backgroundloop.height = 1024;
}

@Override
public void hide() {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {
    prefs.putFloat("Value", value);
    prefs.flush();

    batch.dispose();
    font.dispose();
    bgCity.dispose();
    bgLoop.dispose();
    bgEnd.dispose();
    bgFade.dispose();
    hud.dispose();
    loopmusic.dispose();
    startmusic.dispose();
    endMusic.dispose();
}

}

0 个答案:

没有答案