Adding sound to the Pong game

Copy the assets folder and all its contents from the Chapter 11 folder of the download bundle. Now use your operating system's file explorer to navigate to the Pong/app/src/main folder of your project. Paste the assets folder and its contents.

Obviously, feel free to replace all the sound effects in the assets folder with your own. If you decide to replace all the sound effects, make sure you name them exactly the same or that you make appropriate edits in the code that follows.

Notice that if you use the project explorer window in Android Studio to view the assets folder you can see that the sound effects have been added to the project.

Adding sound to the Pong game

Let's write the code.

Adding the sound variables

At the end of the member variable declarations, before the PongGame constructor, add the following code.

// All these are for playing sounds
private SoundPool mSP;
private int mBeepID = -1;
private int mBoopID = -1;
private int mBopID = -1;
private int mMissID = -1;

Note

You will need to import the SoundPool class using your preferred method or typing the code

import android.media.SoundPool;

The previous code is a declaration for a SoundPool object called mSP and four int variables to hold the IDs of our four sound effects. This is just as we did when we were exploring SoundPool in the previous section.

Note

If you are wondering about the names of the sound files/IDs, I could have called them beep1, beep2 etc but if you say the words out loud one after the other with a slightly different pitch for each the names are quite descriptive. Beep, boop, bop. Apart from miss.

Now we can use what we learned previously to initialize mSP to suit different versions of Android.

Initializing the SoundPool

Add this code which should look quite familiar. Be sure to add the highlighted code before the call to startNewGame.

// Prepare the SoundPool instance
// Depending upon the version of Android
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   AudioAttributes audioAttributes = 
          new AudioAttributes.Builder()
          .setUsage(AudioAttributes.USAGE_MEDIA)
          .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
          .build();

   mSP = new SoundPool.Builder()
        .setMaxStreams(5)
        .setAudioAttributes(audioAttributes)
        .build();
} else {
   mSP = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}


// Everything is ready to start the game
startNewGame();

The previous code checks the version of Android and uses the appropriate method to initialize mSP (our SoundPool).

Note

For this code to work, you will need to import the following classes AssetFileDescriptor, AssetManager, AudioAttributes and AudioManager.

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioAttributes;
import android.media.AudioManager;

Now that we have initialized our SoundPool we can load all the sound effects into it ready for playing. Be sure to add this code immediately after the previous code and before the call to startNewGame.

// Open each of the sound files in turn
// and load them into RAM ready to play
// The try-catch blocks handle when this fails
// and is required.
try{
   AssetManager assetManager = context.getAssets();
   AssetFileDescriptor descriptor;

   descriptor = assetManager.openFd("beep.ogg");
   mBeepID = mSP.load(descriptor, 0);

   descriptor = assetManager.openFd("boop.ogg");
   mBoopID = mSP.load(descriptor, 0);

   descriptor = assetManager.openFd("bop.ogg");
   mBopID = mSP.load(descriptor, 0);

   descriptor = assetManager.openFd("miss.ogg");
   mMissID = mSP.load(descriptor, 0); 

}catch(IOException e){
   Log.d("error", "failed to load sound files");
}

// Everything is ready so start the game
startNewGame();

In the previous code we used a try-catch block to load all the sound effects into the device's RAM memory and associate them with the four int IDs we declared previously. They are now ready to be played.

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

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