Creating 2D Car Racing Game in Android: 6. Adding Music into the Game

Well….! This is the last and final part of this game tutorial. In this part you will learn how to add background music into game using Service class.
So let’s see how to add music….!


Topics Covered

Importing Music File into Project


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3983) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

Before import the music file into Project, create a folder named “raw” inside “res” folder of the Project. Now drag the music file into raw folder. i.e. bgmusic.ogg

Creating 2D Car Racing Game In Android

Creating Background Music Service Class


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3983) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

Create the BGMusic class and extend it by Service class.


Now implement the logic to play music.
Complete BGMusic.java

Now add the service node under application node in AndroidMenifest.xml

Running Music using Thread


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3983) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

Now open MainMenu.java and add following code snippets inside onCreate() function.


Run and test the app to see the result.

Download Complete Source
I hope now you got the basic idea how we can create simple game using OpenGL.

12 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *