audiolibgdxbackground-music

How do I stop a music track from looping in Libgdx


In my project I am attempting to play a music track when the screen switches to the game over screen. It starts when it is supposed to, however, the track continuously loops even when looping is set to false. I tried also using the OnCompletionListener, However I either set it up incorrectly or It just didnt work for some reason or another

This is the variable in my create method.

music = Gdx.audio.newMusic(Gdx.files.internal("gameover.mp3"));

This is the track set up in the render method

        music.setVolume(.2f);
        music.setLooping(false);
        music.play();   

Solution

  • The problem seems to be you're calling play() in the render() method, try calling it on the show() method or use a boolean to flag that you're already playing the music and don't play it again.