-1

Trying to add the facility for user to turn off all sounds in my app using a ui switch. (User preferences)

- (IBAction)toggleaudio:(id)sender {

    if (switchtoggle.on)
    {
        [self.????? play];
    }
    else {
        [self.?????? stop];
    }
}

I'm unsure what to put where the question marks are in my code. What I've tried just gives me errors. If there's a better solution I'm open to suggestions.

My code for the audio:

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"btn2", CFSTR 
                                      ("mp3"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
AudioServicesPlaySystemSound (soundID);
Chris Knadler
  • 2,819
  • 2
  • 26
  • 32
JSA986
  • 5,870
  • 9
  • 45
  • 91

3 Answers3

0

do

    - (IBAction)toggleaudio:(id)sender {

    if (switchtoggle.on)
    {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"btn2", CFSTR 
                                      ("mp3"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
AudioServicesPlaySystemSound (soundID);
    }
    else {
        //nothings playing now
    }

    }
Maximilian Litteral
  • 3,059
  • 2
  • 31
  • 41
  • Thanks for reply, that dosent turn off all the sounds in my app, That just plays the sound, how do I allow the user to turn off all sound effects? – JSA986 Jun 15 '12 at 20:51
  • when the user clicks off, just don't play the sounds? because the iOS doesn't play sounds, your app makes the sounds in code. so have the switch, and the switch can be saved and loaded using NSUserDefaults to check if its on or off – Maximilian Litteral Jun 15 '12 at 21:46
  • Why has this question been marked down, seems a perfectly reasonable question? – JSA986 Jun 15 '12 at 22:02
  • Im still no further forward with this. Are there any tutorials out there anyone can point me to, (no apple ones) – JSA986 Jun 15 '12 at 22:16
  • 1
    @JSA986: You don't seem to understand that your code must be playing the sounds somewhere, and therefore it's up to your code to just not play the sounds if the switch has been turned off, and nobody (who doesn't have your code) could possibly tell you how to do that, beyond "wherever you're calling AudioServicesPlaySystemSound, just don't call it". – abarnert Jun 15 '12 at 22:58
  • That being said, this answer isn't much better than the question, since it's answering "How do I play a sound every other time I click a button?", which is pretty far from your question. I'm not going to -1 anyone, in hopes that you can ask something that makes sense and MaxHasADHD can try to answer whatever it is you actually want to know. – abarnert Jun 15 '12 at 22:59
  • @ abarnert I understand perfectly there is code to make the sounds play as i wrote it and want it there. Ok let me restate in a clearer fashion: How can i give the user the option to turn off all the sounds in my app - if they so choose. I want the sounds to be there as standard, however some users may prefer not to have the bleeps and sound effects etc in my app. For instance in Camera plus or paste bot you have to option to turn off the sounds with a ui switch, my question is how do implement that – JSA986 Jun 15 '12 at 23:33
0

Fixed this using NS User Defaults

JSA986
  • 5,870
  • 9
  • 45
  • 91
0

Try This This will play the sound on pressing the button if switch state on play sound if switch state off do not play the sound..:) .h

@property (strong, nonatomic) IBOutlet UIButton *Btn;
 @property (strong, nonatomic) IBOutlet UISwitch *soundSwitch;
- (IBAction)saveSwitchState:(id)sender;
- (IBAction)ButtonPress:(id)sender

.m

- (IBAction)saveSwitchState:(id)sender
{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([self.soundSwitch isOn])
    {
        [defaults setBool:YES forKey:@"SwitchState"];
    [defaults synchronize];
        NSLog(@"Data Saved");
    }
    else
    {
        [defaults setBool:NO forKey:@"SwitchState"];
        [defaults synchronize];
        NSLog(@"Data Saved");


    }
}
- (void)viewDidLoad//Load Default Switch State
{
    [super viewDidLoad];
 NSString *path = [[NSBundle mainBundle]
     pathForResource:@"ding" ofType:@"mp3"];
     audioPlayer1 = [[AVAudioPlayer alloc]initWithContentsOfURL:
     [NSURL fileURLWithPath:path] error:NULL];

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     self.soundSwitch.on = [defaults boolForKey:@"SwitchState"];
}
- (IBAction)ButtonPress:(id)sender
 if(self.soundSwitch.on==YES)
    {
     [audioPlayer1 play];
    }
    else
    {[audioPlayer1 stop];

    }
}