2013-09-23 52 views
1

我使用下面的代碼來檢查iPhone靜音開關檢測靜音開關ON或OFF: -在iOS的7日發行

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

它工作正常,在iOS 6或以下,但在iOS的7它總是給真值。所以,請任何人告訴,如何解決這個問題。

在此先感謝。

+0

你這個代碼通過IBAction爲在廈門國際銀行連接或故事情節? – karthika

+0

@ karthika: - 不,上面的代碼已經寫入viewDidLoad,然後在按鈕操作檢查silentSwitch打開或關閉。 – Bhrigesh

+0

檢查按鈕操作是否通過TouchUpInside或ValueChanged連接?我也在IOS7中遇到這個問題。 – karthika

回答

3

它不適用於iOS 7的工作,而且也從未在iOS 6中果然奏效,如果你看一下爲什麼它不能在iOS的7工作時,此解決方案是基於相同的代碼,因此,所有信貸的原作者雖然。

  1. 從您的SharkfoodMuteSwitchDetector中保留mute.caf
  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]; 
     } 
    } 
}]; 
+1

當然,它看起來像你的情況,你取決於重複回調,所以你將不得不重新實現該功能(我發現這是一種浪費)。一個簡單的計時器調用ifMute:每秒鐘就足夠了,不會比上一個模式中使用的原始無限重放循環更浪費。 – SG1

+0

這對我很好。非常感謝你:) 獎勵:當手機沒有靜音時(至少在iPhone 5s上),這似乎也大大降低了安靜*嘶嘶聲。 – Warpling