Code the SoundEngine

Fortunately, the SoundEngine class holds no surprises. This is essentially the exact same class as we coded in the Scrolling Shooter project. The only exceptions are that we load different sound effects and the methods which play the sound effects have different names and play different sounds. We have also made it a Singleton to avoid the long parameter lists that we had in the previous project when we passed a SoundEngine reference into so many other classes/methods.

Add the following code for the member variables and the getInstance method.

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import java.io.IOException;

class SoundEngine {
    // for playing sound effects
    private static SoundPool mSP;
    private static int mJump_ID = -1;
    private static int mReach_Objective_ID = -1;
    private static  int mCoin_Pickup_ID = -1;
    private static  int mPlayer_Burn_ID = -1;

    private static SoundEngine ourInstance;

    public static SoundEngine getInstance(Context context) {
        ourInstance = new SoundEngine(context);
        return ourInstance;
    }
}

The getInstance method provides access to the full functionality of the class as well as calling the constructor method to initialize all the sound effects into a SoundPool. Add the constructor method.

public SoundEngine(Context c){
   // Initialize the SoundPool
   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);
   }
   try {
      AssetManager assetManager = c.getAssets();
      AssetFileDescriptor descriptor;

      // Prepare the sounds in memory
      descriptor = assetManager.openFd("jump.ogg");
      mJump_ID = mSP.load(descriptor, 0);

      descriptor = assetManager.openFd("reach_objective.ogg");
      mReach_Objective_ID = mSP.load(descriptor, 0);

      descriptor = assetManager.openFd("coin_pickup.ogg");
      mCoin_Pickup_ID = mSP.load(descriptor, 0);

      descriptor = assetManager.openFd("player_burn.ogg");
      mPlayer_Burn_ID = mSP.load(descriptor, 0);

   } catch (IOException e) {
      // Error
   }
}

Now the sound files are loaded in to memory ready to play. Add the methods that play the sounds, as follows.

public static void playJump(){
   mSP.play(mJump_ID,1, 1, 0, 0, 1);
}

public static void playReachObjective(){
   mSP.play(mReach_Objective_ID,1, 1, 0, 0, 1);
}

public static void playCoinPickup(){
   mSP.play(mCoin_Pickup_ID,1, 1, 0, 0, 1);
}

public static void playPlayerBurn(){
   mSP.play(mPlayer_Burn_ID,1, 1, 0, 0, 1);
}

These methods are public and static so are accessible from anywhere without an instance of SoundEngine.

Any class can now play any of the sound effects.

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

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