There might be an option for you. So in the
.h file where the switch to turn it off, put this code.
@interface SettingScreen : UIViewController<AVAudioPlayerDelegate,AVAudioSessionDelegate>
{
AVAudioPlayer *audioplayer;
}
in .m file
-(void)viewDidLoad
{
NSString* BS_path_blue=[[NSBundle mainBundle]pathForResource:@"Click" ofType:@"mp3"];
audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue] error:NULL];
audioplayer.delegate=self;
[audioplayer prepareToPlay];
UISwitch *soundsButton = [[UISwitch alloc]initWithFrame:CGRectMake(180, 8, 130, 27)];
[self.view addSubview:soundsOnOffButton];
[soundsOnOffButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
soundsButton.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"sound"];
}
-(void)buttonAction:(UIButton *)sender
{
if (soundsButton.on)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"sound"];
[[NSUserDefaults standardUserDefaults] synchronize];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"sound"]==YES)
{
[audioplayer play];
}
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"sound"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
Now if you want to play a sound for something. (Button used as example) in another screen , add delegate and first four lines to your file then add this line to their action.
-(void)buttonAction:(UIButton *)sender
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"sound"]==YES)
{
[audioplayer play];
}
// other code
}
Hope it works.