Ok try this code
On the settings screen .h file
@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 *soundsOnOffButton = [[UISwitch alloc]initWithFrame:CGRectMake(180, 8, 130, 27)]; [self.view addSubview:soundsOnOffButton]; [soundsOnOffButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; soundsOnOffButton.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"sound"]; } -(void)buttonAction:(UIButton *)sender { if (soundsOnOffButton.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]; } }
this is for screen customization. Now, if you want to play the sound for any button on another screen, add the delegate and frist four lines to your file, and then add that line to your action.
-(void)buttonAction:(UIButton *)sender { if ([[NSUserDefaults standardUserDefaults] boolForKey:@"sound"]==YES) { [audioplayer play]; }
Bring me back if a problem occurs.
source share