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...