1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// IBTAudioServicesManager.m
// XFFruit
//
// Created by Xummer on 4/17/15.
// Copyright (c) 2015 Xummer. All rights reserved.
//
#import "IBTAudioServicesManager.h"
#import <objc/runtime.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
#define NEEDS_DISPATCH_RETAIN_RELEASE 0
#else // iOS 5.X or earlier
#define NEEDS_DISPATCH_RETAIN_RELEASE 1
#endif
#define kDefaultDelay 1
@interface IBTAudioServicesManager()
{
BOOL _active;
dispatch_queue_t audioQueue;
}
@end
@implementation IBTAudioServicesManager
static IBTAudioServicesManager *_audioServicesManager = nil;
+ (IBTAudioServicesManager *)sharedManager
{
@synchronized(self)
{
if (nil == _audioServicesManager) {
_audioServicesManager = [[self alloc] init];
}
}
return _audioServicesManager;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self)
{
if (nil == _audioServicesManager) {
_audioServicesManager = [super allocWithZone:zone];
return _audioServicesManager;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (void)dealloc
{
#if NEEDS_DISPATCH_RETAIN_RELEASE
if (audioQueue)
dispatch_release(audioQueue);
#endif
}
- (id)init
{
self = [super init];
if (self) {
audioQueue = dispatch_queue_create(class_getName([self class]), NULL);
self.delay = kDefaultDelay;
}
return self;
}
- (void)playSound:(SystemSoundID)soundID {
if (_active) {
return;
}
__block SystemSoundID sound = soundID;
dispatch_async(audioQueue, ^{ @autoreleasepool {
// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
_active = YES;
AudioServicesPlayAlertSound(sound);
}});
dispatch_time_t waitTime = dispatch_time(DISPATCH_TIME_NOW, _delay * NSEC_PER_SEC);
dispatch_after(waitTime, dispatch_get_main_queue(), ^(void) {
_active = NO;
});
}
//
static bool BSTAudioServicesActive;
+ (void)setBSTAudioServicesActive:(NSNumber*)activeNum
{
BSTAudioServicesActive = [activeNum boolValue];
}
+ (void)playSound:(SystemSoundID)soundID {
if (!BSTAudioServicesActive) {
// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
BSTAudioServicesActive = YES;
AudioServicesPlayAlertSound(soundID);
[self performSelector:@selector(setBSTAudioServicesActive:) withObject:[NSNumber numberWithBool:NO] afterDelay:2.0f];
}
}
@end