2015-12-05 21 views
8

Như AudioSessionInitializeAudioSessionGetProperty đang bị phản đối, tôi nhận được các giá trị trở lại sai:iOS 9 Cách phát hiện chế độ im lặng?

CFStringRef state = nil; 
UInt32 propertySize = sizeof(CFStringRef); 
AudioSessionInitialize(NULL, NULL, NULL, NULL); 
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state); 
[[AVAudioSession sharedInstance] setActive:YES error:nil]; 
if (status == kAudioSessionNoError) { 
    return CFStringGetLength(state) == 0; // YES = silent 
} 
return NO; 

Từ mã này (tôi thấy nó here), tôi nhận được kết quả không chính xác như nhau bất kể những gì nhà nước thực sự là thiết bị trên. Làm thế nào tôi có thể phát hiện nếu chế độ im lặng đang BẬT trên thiết bị ngay bây giờ?

Trả lời

5

API không còn khả dụng nữa. Nhưng công việc xung quanh rất đơn giản:

  • Chơi một âm thanh ngắn và phát hiện thời điểm đó nó kết thúc chơi
  • Nếu thời gian mà nó kết thúc chơi ngắn hơn độ dài thực tế của âm thanh, so với các thiết bị sẽ tắt

Lừa đảo đã đăng lớp trợ giúp MuteChecker trên số blog của mình. Sử dụng nó như sau:

self.muteChecker = [[MuteChecker alloc] initWithCompletionBlk:^(NSTimeInterval lapse, BOOL muted) { 
    NSLog(@"muted: %d", muted); 
}]; 
[self.muteChecker check]; 

Đây là mã hoàn chỉnh cho các lớp, bạn có thể đơn giản copy qua để dự án của bạn:

MuteChecker.h

#import <Foundation/Foundation.h> 
#import <AudioToolbox/AudioToolbox.h> 

typedef void (^MuteCheckCompletionHandler)(NSTimeInterval lapse, BOOL muted); 

// this class must use with a MuteChecker.caf (a 0.2 sec mute sound) in Bundle 
@interface MuteChecker : NSObject 
-(instancetype)initWithCompletionBlk:(MuteCheckCompletionHandler)completionBlk; 
-(void)check; 
@end 

MuteChecker. cpp

#import "MuteChecker.h" 

void MuteCheckCompletionProc(SystemSoundID ssID, void* clientData); 

@interface MuteChecker() 
@property (nonatomic,assign) SystemSoundID soundId; 
@property (strong) MuteCheckCompletionHandler completionBlk; 
@property (nonatomic, strong)NSDate *startTime; 
-(void)completed; 
@end 

void MuteCheckCompletionProc(SystemSoundID ssID, void* clientData){ 
    MuteChecker *obj = (__bridge MuteChecker *)clientData; 
    [obj completed]; 
} 

@implementation MuteChecker 

-(void)playMuteSound 
{ 
    self.startTime = [NSDate date]; 
    AudioServicesPlaySystemSound(self.soundId); 
} 

-(void)completed 
{ 
    NSDate *now = [NSDate date]; 
    NSTimeInterval t = [now timeIntervalSinceDate:self.startTime]; 
    BOOL muted = (t > 0.1)? NO : YES; 
    self.completionBlk(t, muted); 
} 

-(void)check { 
    if (self.startTime == nil) { 
     [self playMuteSound]; 
    } else { 
     NSDate *now = [NSDate date]; 
     NSTimeInterval lastCheck = [now timeIntervalSinceDate:self.startTime]; 
     if (lastCheck > 1) { //prevent checking interval shorter then the sound length 
      [self playMuteSound]; 
     } 
    } 
} 

- (instancetype)initWithCompletionBlk:(MuteCheckCompletionHandler)completionBlk 
{ 
    self = [self init]; 
    if (self) { 
     NSURL* url = [[NSBundle mainBundle] URLForResource:@"MuteChecker" withExtension:@"caf"]; 
     if (AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_soundId) == kAudioServicesNoError){ 
      AudioServicesAddSystemSoundCompletion(self.soundId, CFRunLoopGetMain(), kCFRunLoopDefaultMode, MuteCheckCompletionProc,(__bridge void *)(self)); 
      UInt32 yes = 1; 
      AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(_soundId),&_soundId,sizeof(yes), &yes); 
      self.completionBlk = completionBlk; 
     } else { 
      NSLog(@"error setting up Sound ID"); 
     } 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    if (self.soundId != -1){ 
     AudioServicesRemoveSystemSoundCompletion(self.soundId); 
     AudioServicesDisposeSystemSoundID(self.soundId); 
    } 
} 

@end 

Lưu ý quan trọng: bạn cũng sẽ phải cung cấp một đoạn âm thanh ngắn MuteChecker.caf để mã hoạt động. Bạn có thể tải xuống từ blog của mình trực tiếp hoặc tự tạo một blog.

Các vấn đề liên quan