i want to make iOS app with play sound on every button tap, and i can disable/Enable sound from setting screen. Can any one help me on this?
Asked
Active
Viewed 2,803 times
3 Answers
2
Ok, try this code
In your setting 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 setting screen. Now if you want to play a sound for any button in another screen , add delegate and frist four line to your that file then add this line to their action.
-(void)buttonAction:(UIButton *)sender
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"sound"]==YES)
{
[audioplayer play];
}
// other code
}
Revert me if any problem.

Ajay Chaudhary
- 1,993
- 15
- 23
-
thanks for answering it might work but do i have to check it for every button click? there is no generic approach of doing this so that when i set audio ON it play and when i turn it OFF dont play... – abdus.me Dec 03 '12 at 11:40
-
This will works great. Accept it as right answer if it solve your problem. – Ajay Chaudhary Dec 04 '12 at 04:39
1
Just check the setting before playing any sound..?
// pseudo code
- (void)playSound:(NSInteger)soundID;
{
if(settings.soundEnabled) {
[SoundPlayer playSoundWithID:soundID];
}
}

calimarkus
- 9,955
- 2
- 28
- 48
-
thanks for quick answer, so do i have to check it on every button click? there is no better approach of doing this? – abdus.me Dec 03 '12 at 11:36
-
What do you think how this works internally, if there was a setting like this? Someone needs to check the settings. Just use the same method to play a sound everywhere and within that method check your setting. – calimarkus Dec 03 '12 at 11:51
0
You cant change the Device sound(app will be rejected) how ever you can set volume By assigning some values between 0 to 1 .in your setting option if user selects disable then set it to 0. check sam's answer here : How to disable iOS System Sounds

Community
- 1
- 1

Siba Prasad Hota
- 4,779
- 1
- 20
- 40
-
well in that case, volume set will be for whole phone or only for my app? – abdus.me Dec 03 '12 at 11:43