-1

尝试为用户添加功能以使用 ui 开关关闭我的应用程序中的所有声音。(用户偏好)

- (IBAction)toggleaudio:(id)sender {

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

我不确定在我的代码中问号的位置应该放什么。我尝试过的只是给了我错误。如果有更好的解决方案,我愿意接受建议。

我的音频代码:

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"btn2", CFSTR 
                                      ("mp3"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
AudioServicesPlaySystemSound (soundID);
4

3 回答 3

0

    - (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
    }

    }
于 2012-06-15T20:22:25.290 回答
0

使用 NS 用户默认值修复此问题

于 2012-07-02T12:00:37.860 回答
0

试试这个这将在按下按钮时播放声音如果开关状态打开播放声音如果开关状态关闭不播放声音..:) .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];

    }
}
于 2014-02-15T06:13:45.863 回答