Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would go for a <a href="http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh" rel="nofollow">Singleton</a> class that could manage all the sounds in your activity. I haven't previously done in Android, but on C++, and I've seen in other examples (<a href="http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html" rel="nofollow">here</a> and <a href="http://www.droidnova.com/creating-sound-effects-in-android-part-2,695.html" rel="nofollow">here</a>) that actually support my idea.</p> <p>A Singleton is accessible from everywhere in your application and more importantly, there's only one instance per application, which will let you configure any sound globally. </p> <p>What those links and me are telling is that having a single class that manages every sound in your game will help you: </p> <ol> <li>simplify sound calls <em>(you can call every sound from a <code>HashMap</code> (for example) by <code>YourSoundManager.getInstance().play("soundName");</code> and stop them by <code>YourSoundManager.getInstance().stop("soundName");</code>, or simply copy-pasting the examples above to have a little more control in speed and sounds)</em> by <a href="http://en.wikipedia.org/wiki/Facade_pattern" rel="nofollow">facading</a> the calls and,</li> <li>control any sound, just like you had a big mixing console, except it is done using nothing but code :)</li> </ol> <p>From those examples it depends on what you want to do to mute your game. Either stopping all the sounds, or putting their volume down. <strong>I'd go for stopping them at first</strong>, as it's the simplest approach.</p> <p><strong>EDIT:</strong></p> <p>If you want to stop a sound when switching from an activity to another one, you can still use the singleton approach.</p> <p>You can override the onResume() method so it plays the sound you are willing to use. Also check that this sound isn't playing by looking up at a HashMap that could contain any playing sound (or use other more familiar tactics). I'll write you some code that may not be working to you (as it depends on your final approach and how you manage sounds)</p> <pre><code>@Override public void onResume() { if(! SoundManager.getInstance().isPlaying("thisStageSound") ) //if it's already playing, do not stop it! { SoundManager.getInstance().stopAllSounds(); SoundManager.getInstance().playSound("thisStageSound"); } super.onResume(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload