Adding Music

Music for Android is exciting. Some interesting technology provides amazing functionality. Before you look into these options, let’s play a MIDI song file during game play. Your old friend the MediaPlayer class performs this perfectly because it’s designed to play all the media files in Android.

Although music is generally longer than a sound effect, and you use a different file format, it’s handled nearly identically to the sound effects in the previous sections.

To get free sound effects, you went to www.freesounds.org. For MIDI audios, I use www.midiworld.com. The site provides a large library of .midi files that you can use in your own creations. Under the Pop category, I found “Take a Chance on Me” by ABBA. Let’s add it to your app:

  1. Download “Take a Chance on Me” (or the song of your choice) to your desktop. Note that if you have your own MIDI files, Android is picky about using the .mid extension as opposed to .midi. In the future, Android may offer support for both, but this has been a common source of issues in the past.
  2. Just like a sound effect, drag or copy the .mid file into the res  raw folder of your project. Before you do this, give it a sensible name that is easy to retype. I renamed the file background_music.mid for now.
  3. With the resource properly stored, you can look at the simple code that is used to run it. First create a private MediaPlayer variable at the beginning of the GameView class:
    private MediaPlayer mp;
  4. Add the following bolded code to the surfaceCreated() method. This is your way of instructing the tablet to start the music as soon as the screen image is created:
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
            mGameLogic.setGameState(mGameLogic.RUNNING);
            mGameLogic.start();

            mp = MediaPlayer.create(getContext(), R.raw.background_music);
            mp.setLooping(true);
            mp.start();
    }
  5. Because you’ve already dealt with a media player, this code should be self-explanatory. The MediaPlayer object is created by loading the proper file and passing the context of the application. You do this to prepare the music for playing. You then tell MediaPlayer to loop the sample before starting it.
  6. To clean up when you’re finished, change the surfaceDestroyed() function with the following code:
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
            soundPool.release();
            mp.stop();
            mp.release();
    }
  7. That’s all there is to it.
  8. Run the SoundsTest application, and you should hear music when the game starts. If you drag your cursor on the screen, the sounds from your soundPool play along with the music. You can use the method you’ve created whenever you want to play a music file in a game.

With the ability to play sound effects as well as music, you’ve finished your exploration of audio for Android games. The next important media object is, of course, video. The following section covers how to play a clip during a game. Because movies are media, they’re handled much the same as sounds.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.65