它不適用於iOS 7的工作,而且也從未在iOS 6中果然奏效,如果你看一下爲什麼它不能在iOS的7工作時,此解決方案是基於相同的代碼,因此,所有信貸的原作者雖然。
- 從您的SharkfoodMuteSwitchDetector中保留
mute.caf
。
- 創建一個新的類,稱爲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-25 19:34:29
SG1
你這個代碼通過IBAction爲在廈門國際銀行連接或故事情節? – karthika
@ karthika: - 不,上面的代碼已經寫入viewDidLoad,然後在按鈕操作檢查silentSwitch打開或關閉。 – Bhrigesh
檢查按鈕操作是否通過TouchUpInside或ValueChanged連接?我也在IOS7中遇到這個問題。 – karthika