2
\$\begingroup\$

I have a easy and specific question.

Let's say that in my game I use singleton for playing/pausing/changing volume of specific sounds (SimpleAudioEngine in cocos2d-x).

What is the best way to handle mute button, which is displayed in the upper corner of the screen? Should I just volume everything down and do not care about checking the state of this setting before playing any kind of sound?

1) This is called when an object is destroyed

if (!UserDefault::getInstance()->getBoolForKey(MUTED, false))
{
    SimpleAudioEngine::getInstance()->playEffect("shouting.ogg", /* other arguments */)
}

I think that not playing a sound at all is bad practise (if muted is on - like in the above example), because when the user changes this setting while eg. his hero is getting shot, nothing will be played - even if it supposed to take 5 seconds and the option was changed after 2,5 seconds.

But if I only volume everything down - then the rest of the shout will be heard.

\$\endgroup\$
1
\$\begingroup\$

Adding to ZEKE answer.

There are situations where changing a setting gives a temporary weird behaviour, and that is acceptable. In this case, really, it depends.

Is your game based on the sound? Do you expect the player to use the mute/un-mute feature often?

If the answer is yes to any of these, you'd probably want to play the sound, but set the volume to 0 when it is muted in any case. In this case, the "weird behaviour" could be an issue.

If the answer to both of these question is no, I would suggest you wrap the SimpleAudioEngine into one of your classes (for instance MyAudioEngine) that will relay calls to it. In this case, the "weird behaviour" is probably not an issue.

This class will offer the same services, but the "play" will be different, the "play" will check if the sound is muted by the user, and will not play it if the user has muted the game; the if you wrote in the OP will be encapsulated in the play function. This will allow the end user classes to just don't care about the mute state, (because, in fact, they don't care) and the code that calls the play sound methods will be cleaner. With this you'll still have to set the volume to 0 for sounds that are already playing when the user wants to mute.

This said, since the library you're using seems to be open-source, you could go in there and add the feature directly to SimpleAudioEngine. The advantage would be that you'd have one less class to build and carry around, and all your code base would work right away. The downsides to it would be that if you update the 3rd party library, you face chances of changes in the original code base and you'd have to re-implement the feature in the new version.

\$\endgroup\$
1
\$\begingroup\$

I'm not exactly sure what you mean by:

his hero is getting shot, nothing will be played - even if it supposed to take 5 seconds and the option was changed after 2,5 seconds.

If you only check for muted when you first play a sound, it will continue to play even after you pressed the mute button.

The solution for this, is setting the volume of the sound to zero when you press the mute button. This way; the sound will be muted, and you won't need to create unnecessary sounds.

TL;DR: Do both.

\$\endgroup\$
  • \$\begingroup\$ I check for muted every time. But if the sound is - let's say - 5 seconds long and the user pressed mute button in the middle (eg. after 2 seconds) of the sound being played, then I have two options: either pause the sound (what I think makes no sense) or volume it down. Because player can immediately press button again (eg. after 1 second of the sound being muted) and sound is supposed to be played from the 3rd second, not from the 2nd. Does it make sense? ;) \$\endgroup\$ – y434y Nov 6 '15 at 12:18
  • \$\begingroup\$ @y43y Yes, pausing a sound does not make sense here; set the volume for existing sounds to zero instead. :] \$\endgroup\$ – ZEKE Nov 6 '15 at 13:54
  • \$\begingroup\$ Some sounds can be quite long (ambient sounds), or can be looping (something burning). If you prefer pause over volume, some objects or ambience may lose their sounds when coming back from mute state. \$\endgroup\$ – Guney Ozsan Nov 19 '15 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.