Book Image

Mastering Android Game Development

By : Raul Portales
Book Image

Mastering Android Game Development

By: Raul Portales

Overview of this book

Table of Contents (18 chapters)
Mastering Android Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
API Levels for Android Versions
Index

SoundManager


To manage sounds and music, we are going to have a class called SoundManager. This class will be instantiated only once along the Application code and it will be done at onCreate of YassActivity. There are several reasons to do this:

  • Sound effects do take a little time to be loaded, so it is better to load them in advance

  • We may want to use sounds and music in the menus

  • Loading sounds and music requires memory; it does not make sense to duplicate that

Let's look at the modifications we need to make to YassActivity:

private SoundManager mSoundManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  [...]
  setVolumeControlStream(AudioManager.STREAM_MUSIC);
  mSoundManager = new SoundManager(getApplicationContext());
}

public SoundManager getSoundManager() {
  return mSoundManager;
}

We have a field that contains the SoundManager, and we initialize it during the onCreate method. We also provide a getter method for it.

There is...