2

我正在使用以下代码来检查 iPhone 静音开关是打开还是关闭:-

if (self)
   {
    self.detector = [SharkfoodMuteSwitchDetector shared];
    CheckInViewController* sself = self;
    self.detector.silentNotify = ^(BOOL silent)
       {
        [sself.silentSwitch setOn:silent animated:YES];
      };
   }

它在 iOS 6 及更低版本中运行良好,但在 iOS 7 中它总是给出 TRUE 值。所以,请任何人告诉,如何解决这个问题。

提前致谢。

4

1 回答 1

3

它在 iOS 7 中不起作用,并且如果您查看它在 iOS 7 中不起作用的原因,它在 iOS 6 中从未真正起作用。这个解决方案基于相同的代码,因此所有归功于原作者。

  1. 远离mute.caf您的 SharkfoodMuteSwitchDetector。
  2. 创建一个新类,称为 HASilentSwitchDetector(或其他),或替换 SharkfoodMuteSwitchDetector 中的代码。

在头文件中:

#import <AudioToolbox/AudioToolbox.h>

typedef void(^HASilentSwitchDetectorBlock)(BOOL success, BOOL silent);

@interface HASilentSwitchDetector : NSObject

+ (void)ifMute:(HASilentSwitchDetectorBlock)then;

@end

在实现文件中:

#import "HASilentSwitchDetector.h"

void MuteSoundPlaybackComplete(SystemSoundID ssID, void *clientData)
{
    //Completion
    NSDictionary *soundCompletion = CFBridgingRelease(clientData);

    //Mute
    NSTimeInterval interval = [soundCompletion[@"interval"] doubleValue];
    NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - interval;
    BOOL isMute = elapsed < 0.2; // mute.caf is .2s long...

    //Then
    HASilentSwitchDetectorBlock then = soundCompletion[@"then"];
    then(YES, isMute);

    //Cleanup
    SystemSoundID soundID = [soundCompletion[@"soundID"] integerValue];
    AudioServicesRemoveSystemSoundCompletion(soundID);
    AudioServicesDisposeSystemSoundID(soundID);
}

@implementation HASilentSwitchDetector

+ (void)ifMute:(HASilentSwitchDetectorBlock)then
{
    //Check
    if ( !then ) {
        return;
    }

    //Create
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"mute" withExtension:@"caf"];
    SystemSoundID soundID;
    if ( AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID) == kAudioServicesNoError ) {
        //UI Sound
        UInt32 yes = 1;
        AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(soundID), &soundID,sizeof(yes), &yes);

        //Callback
        NSDictionary *soundCompletion = @{@"then" : [then copy], @"soundID" : @(soundID), @"interval" : @([NSDate timeIntervalSinceReferenceDate])};
        AudioServicesAddSystemSoundCompletion(soundID, CFRunLoopGetMain(), kCFRunLoopDefaultMode, MuteSoundPlaybackComplete, (void *)CFBridgingRetain(soundCompletion));

        //Play
        AudioServicesPlaySystemSound(soundID);
    } else {
        //Fail
        then(NO, NO);
    }
}

@end

像这样使用:

[HASilentSwitchDetector ifMute:^(BOOL success, BOOL silent) {
    if ( success ) {
        if ( ![[NSUserDefaults standardUserDefaults] boolForKey:forKey:kHasShownMuteWarning] && silent ) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kHasShownMuteWarning];
            [[[UIAlertView alloc] initWithTitle:[@"Mute Warning" localized] message:[NSString stringWithFormat:[@"This %@'s mute switch is on.  To ensure your alarm will be audible, unmute your device." localized], [[[UIDevice currentDevice] isiPad]? @"iPad" : @"iPhone" localized]] delegate:nil cancelButtonTitle:nil otherButtonTitles:[@"Ok" localized], nil] show];
        }
    }
}];
于 2013-09-25T19:34:29.010 回答