Created
May 10, 2023 02:27
-
-
Save DargonLee/ab5fa52ddc1b05902eeda033fab6b21d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)addVolumeObserver | |
{ | |
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; | |
NSError *error = nil; | |
[audioSession setActive:YES error:&error]; | |
if (error) { | |
NSLog(@"%@", error); | |
} | |
[audioSession addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; | |
} | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context | |
{ | |
if (![keyPath isEqualToString:@"outputVolume"]) { | |
return; | |
} | |
if (self.start == nil) { | |
// 时间1 | |
NSDate *date1 = [NSDate date]; | |
NSTimeZone *zone1 = [NSTimeZone systemTimeZone]; | |
NSInteger interval1 = [zone1 secondsFromGMTForDate:date1]; | |
self.start = [date1 dateByAddingTimeInterval:interval1]; | |
} | |
self.timeCount ++; | |
NSKeyValueChangeKey newValue = change[@"new"]; | |
NSKeyValueChangeKey oldValue = change[@"old"]; | |
NSLog(@"oldValue=%@ newValue=%@", oldValue, newValue); | |
// 时间2 | |
NSDate *date2 = [NSDate date]; | |
NSTimeZone *zone2 = [NSTimeZone systemTimeZone]; | |
NSInteger interval2 = [zone2 secondsFromGMTForDate:date2]; | |
self.end = [date2 dateByAddingTimeInterval:interval2]; | |
double intervalTime = [self.end timeIntervalSinceReferenceDate] - [self.start timeIntervalSinceReferenceDate]; | |
NSLog(@"时间差 = %f", intervalTime); | |
if (intervalTime > 5.0) { | |
[self resetCount]; | |
return; | |
} | |
if (self.timeCount == 3) { | |
NSLog(@"AlertShow"); | |
[self resetCount]; | |
[self showAlertView]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment