Adding Background Music

Adding music and sound effects to your game is pretty easy. LibGDX has 2 classes called Music and Sound that handle streaming music and sound effects. I will show you how to play music using the Music class. If you like you can read up on Sound effects and Streaming Music in the LibGDX wiki:

Now before we add our code here is the download link for the music of Mam Bow 2's level 1. Download it and add it to the Assets folder.

Click to Download

Now go to your main class and create a field for your Music object and import LibGDX's Music class (above the create method).

Subsequently add the following lines of code to the create() method

Line 1: loads the music file into memory. By default Gdx.files.internal uses the Assets folder in the Android project as the projects root folder for assets.
Line 2: .setLooping(true) loops the music file
Line 3: calling the .play() starts the music playback.

And that's all there is to it. In the next section we're going to work on the collision detection between the player and those parts of the tilemap the spaceship should crash when it moves over those parts. To do this we'll create a collision layer in Tiled, load it in our project and some code that check whether the spaceship if flying over areas it shouldn't.
This will be a little bit more challenging. See you in the next section.



-->