How do I add a Mute button to SpriteKit / Objective C?

I am programming a small SpriteKit game and I want to add a mute button so you can listen to your own music while playing. Currently I am just stopping the audio playback for the scene that has the mute button (main menu). However, my game still automatically stops the user's music even if the audio is stopped. How can I prevent this?

Also, I would like to convey information that the mute button is pressed (located in the main menu) in my game scene so that I can mute the music there, how do I do this? Basically how to pass the value of a variable from one scene to another?

+3


source to share


1 answer


What I did a while ago was to check if the iPod Player is working and give it priority over game music. (i.e. check if the iPod is playing to start playing with music, as the iPod music player will usually overwrite your game if you are using AVAudioPlayer.

+ (bool) IsIpodPlaying{
    if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying
        || [[MPMusicPlayerController applicationMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying){
        NSLog(@"iPod IS playing");
        return true;
    }else{
        NSLog(@"iPod NOT playing");
        return false;
    }
}

      

You can obviously do the same for your mute button. Ignore any action if the iPod is playing, otherwise stop your game.



To transmit information when you press the Mute button, you can (in order of personal preference):

  • Set your menu scene as a delegate to your game scene and create a way to enable or disable sounds (for example userDidEnableSound:

    )
  • Create a singleton class of the SoundControl class that has the ability to enable and disable sound methods and access them wherever you want.
  • Send the notification with help NSNotificationCenter

    and wait for it in the menu.
+1


source







All Articles