-1

I'm very new to iOS development. I've currently got an app with some SFX in there. I've found a couple of other threads which explain the approach / logic, which I full understand, but it's the code I'm after as I have no idea of the syntax.

If I were to use a UI Switch, how would I turn off any SFX that's used within the app?

Any help and assistance would be greatly appreciated.

Thank you all.

Tron
  • 43
  • 7

2 Answers2

0

How are you using your sfx? If you could upload your code it would help. But you can try this?

- (IBAction)SwitchValueChanged:(id)sender
{

    if([sender isOn])
{
        NSLog(@"Switch is ON");
        //On Your SFX
    } 
    else
{
        NSLog(@"Switch is OFF");
        //Off Your SFX
    }

}

I don't really understand what you mean by Some SFX So its hard to provide help

Dalton Ng
  • 55
  • 5
  • Hi Dalton - This is great, very kind of you. I haven't written anything yet, I'm trying to formalise it in my head. (Very new to it all). However, the code you have provided is along the lines of how i would have approached it. Being new to objectiveC, i was curious to know if there was a general line that turned off all sounds that are running within the app (where your inline comments are currently placed) – Tron Sep 15 '14 at 10:10
  • How are you playing the sfx? – Dalton Ng Sep 15 '14 at 10:13
0

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.

Dalton Ng
  • 55
  • 5