【问题标题】:turn off sounds in app using ui switch xcode使用ui switch xcode关闭应用程序中的声音
【发布时间】:2012-06-15 20:03:37
【问题描述】:

尝试添加功能,让用户使用 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);

【问题讨论】:

    标签: objective-c audio ios5 xcode4.3


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,我的应用程序中的所有声音都关闭了,只是播放声音,如何让用户关闭所有声音效果?
    • 当用户点击关闭时,只是不播放声音?因为 iOS 不播放声音,所以您的应用程序会在代码中发出声音。开关也是如此,并且可以使用 NSUserDefaults 保存和加载开关以检查它是打开还是关闭
    • 为什么这个问题被标记下来,似乎是一个完全合理的问题?
    • 我仍然没有进一步前进。有没有任何人可以指点我的教程,(没有苹果的)
    • @JSA986:您似乎不明白您的代码必须在某处播放声音,因此如果开关已关闭,则取决于您的代码是否不播放声音,并且没有人(没有您的代码)可能会告诉您如何做到这一点,除了“无论您在哪里调用 AudioServicesPlaySystemSound,都不要调用它”。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-31
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多