尝试为用户添加设施,使用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);
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
使用NS用户默认值修复此问题
答案 2 :(得分: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];
}
}