Mustafa
Mustafa

Reputation: 775

xcoded mute button on toolbar to mute sound just of the App

I am brand new to iOS development. I have a simple 'clicker' app which makes a sound (mp3 file) every time you click an on screen button.

I want to implement a mute button which when selected:

  1. The button image changes (and stays changed until selected again)

  2. The sound of just the app is muted

  3. On selecting again returns to original state - sound on and previos button image

Here is how I'm playing my sound:

- (IBAction)click;


#import "soundButtonViewController.h"

@implementation soundButtonViewController

-(IBAction)click
{
   CFBundleRef mainBundle = CFBundleGetMainBundle();
   CFURLRef soundFileURLRef;
   soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
   (CFStringRef)@"click",CFSTR ("mp3"), NULL);
   UInt32 soundID;
   AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
   AudioServicesPlaySystemSound(soundID);
}

Is there any example code of this anywhere? I have done some research and there seems to be a few methods out there, but was wanting specific to my requirements. Any help or direction would be greatly appreciated. Thanks.

Upvotes: 2

Views: 1499

Answers (1)

Fluffhead
Fluffhead

Reputation: 845

For changing the button, my usual way of doing this is to create a BOOL to keep track of the state of the button. In this case, the BOOL is "muteOn" and the IBOutlet for the Button is muteButton. Then, I use this code to control the button image and whether or not the app is muted. You can also put an if/then inside your click action (also shown below) to either play a sound or Do Nothing, if muteON is set to YES. Remember to declare your Bool in the .h file, too.

-(IBAction)muteToggle{

if (muteOn == YES) {
    muteOn = NO;
    UIImage *buttonImage = [UIImage imageNamed:@"muteOff.png"];
    [muteButton setImage:buttonImage forState:UIControlStateNormal];
}

else {
    muteOn = YES;
    UIImage *buttonImage = [UIImage imageNamed:@"muteOn.png"];
    [muteButton setImage:buttonImage forState:UIControlStateNormal];
}

}


-(IBAction) click {

 if (muteOn == YES){
//Do Nothing
 }

 else{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
 (CFStringRef)@"click",CFSTR ("mp3"), NULL);
  UInt32 soundID;
 AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
  AudioServicesPlaySystemSound(soundID);

}
}

Upvotes: 1

Related Questions