IOS application with the ability to enable and disable sound

I want to make an iOS app with sound every time I press a button, and I can mute / unmute the sound from the settings screen. Can someone help me with this?

+4
source share
3 answers

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]; } // other code } 

Bring me back if a problem occurs.

+2
source

Just check the setting before playing any sound.?

 // pseudo code - (void)playSound:(NSInteger)soundID; { if(settings.soundEnabled) { [SoundPlayer playSoundWithID:soundID]; } } 
+1
source

It is not possible to change the sound of the device (the application will be rejected), as you can set the volume. When assigning some values ​​from 0 to 1. In the parameter settings, if the user selects the mute function, set the value to 0. check sam answer here: How to disable iOS sound systems

0
source

All Articles