在iOS 7问题中检测静音开关 [英] Detecting Silent Switch in iOS 7 issue

查看:346
本文介绍了在iOS 7问题中检测静音开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码检查iPhone静音开关是ON还是OFF: -

I am using following code to check iPhone silent switch is ON or OFF :-

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值。所以,请任何人告诉,如何解决这个问题。

It works fine in iOS 6 and below but in iOS 7 it always gives TRUE value. So, Please any one tell, how to resolve this issue.

提前致谢。

推荐答案

它不起作用iOS 7,如果你看看为什么它在iOS 7中不起作用,它从未在iOS 6中真正起作用。这个解决方案基于相同的代码,所以所有这些都归功于原作者。

It doesn't work in iOS 7, and it never really worked in iOS 6 if you look at why it doesn't work in iOS 7. This solution is based on the same code, so all credit to the original author though.


  1. 从SharkfoodMuteSwitchDetector保留 mute.caf

  2. 创建一个新的class,称为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];
        }
    }
}];

这篇关于在iOS 7问题中检测静音开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆