@@ -21,6 +21,7 @@ @interface LPDSoundService () <AVAudioPlayerDelegate>
21
21
@property (nonatomic , strong ) NSMutableArray <NSString *> *cacheSounds;
22
22
@property (nonatomic , assign ) BOOL isPlaying;
23
23
@property (nonatomic , assign ) BOOL needCache;
24
+ @property (nonatomic , assign ) BOOL isInterrupt;
24
25
25
26
@end
26
27
@@ -38,17 +39,19 @@ + (instancetype)sharedInstance {
38
39
- (instancetype )init {
39
40
self = [super init ];
40
41
if (self) {
41
- [LPDTeleponyManager sharedInstance ];
42
42
AVAudioSession *session = [AVAudioSession sharedInstance ];
43
- [session setCategory: AVAudioSessionCategoryPlayAndRecord withOptions: AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionAllowBluetooth error: nil ];
43
+ [session setCategory: AVAudioSessionCategoryPlayback withOptions: AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionAllowBluetooth error: nil ];
44
44
45
45
self.cacheSounds = [NSMutableArray array ];
46
46
self.canShake = YES ;
47
47
self.needCache = YES ;
48
- [self setupNotifications ];
49
48
[[NSNotificationCenter defaultCenter ] addObserver: self
50
49
selector: @selector (playCacheSoundWhenApplicationDidBecomeActive )
51
50
name: UIApplicationDidBecomeActiveNotification object: nil ];
51
+ [[NSNotificationCenter defaultCenter ] addObserver: self
52
+ selector: @selector (interrupted: )
53
+ name: AVAudioSessionInterruptionNotification
54
+ object: [AVAudioSession sharedInstance ]];
52
55
}
53
56
return self;
54
57
}
@@ -69,15 +72,12 @@ - (void)playSoundWithName:(NSString *)soundName ofType:(NSString*)type {
69
72
}
70
73
return ;
71
74
}
72
- if (self.needCache == YES ) {
73
- self.isPlaying = YES ;
74
- }
75
75
76
76
// 让app支持接受远程控制事件
77
77
[[UIApplication sharedApplication ] beginReceivingRemoteControlEvents ];
78
78
79
79
[self setOutputWith: [AVAudioSession sharedInstance ]];
80
- [[AVAudioSession sharedInstance ] setActive: YES error: nil ];
80
+ [[AVAudioSession sharedInstance ]setActive:YES withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error: nil ];
81
81
82
82
NSURL *fileURL = [NSURL fileURLWithPath: [[NSBundle mainBundle ] pathForResource: soundName ofType: type]];
83
83
self.audioPlayer = [[AVAudioPlayer alloc ] initWithContentsOfURL: fileURL error: nil ];
@@ -131,9 +131,12 @@ - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)f
131
131
[self .audioPlayer stop ];
132
132
self.audioPlayer = nil ;
133
133
NSLog (@" sound finish" );
134
+ NSError *error;
134
135
[[AVAudioSession sharedInstance ] setActive: NO
135
- withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
136
- error: nil ];
136
+ error: &error];
137
+ if (error) {
138
+ NSLog (@" %@ " ,error.localizedDescription );
139
+ }
137
140
if (self.isPlaying ) {
138
141
self.isPlaying = NO ;
139
142
[self playCacheSound ];
@@ -168,27 +171,25 @@ - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)
168
171
}
169
172
}
170
173
171
- - (void )setupNotifications {
172
- [[NSNotificationCenter defaultCenter ] addObserver: self
173
- selector: @selector (interrupted: )
174
- name: AVAudioSessionInterruptionNotification
175
- object: [AVAudioSession sharedInstance ]];
176
- }
177
-
178
174
-(void )interrupted : (NSNotification *)notification {
179
175
NSDictionary *userInfo = notification.userInfo ;
180
- if (userInfo.count > 0 ) {
181
- if ([[userInfo objectForKey: AVAudioSessionSilenceSecondaryAudioHintTypeKey] isKindOfClass: [NSNumber class ]]) {
182
- AVAudioSessionSilenceSecondaryAudioHintType typeValue = [[userInfo objectForKey: AVAudioSessionSilenceSecondaryAudioHintTypeKey] integerValue ];
183
- if (typeValue == AVAudioSessionSilenceSecondaryAudioHintTypeBegin) {
184
- if ([self .audioPlayer isPlaying ]) {
185
- [self .audioPlayer pause ];
186
- }
187
- } else {
176
+ if (!userInfo) { return ; }
177
+
178
+ if ([[userInfo objectForKey: AVAudioSessionInterruptionTypeKey] isKindOfClass: [NSNumber class ]]) {
179
+ AVAudioSessionInterruptionType typeValue = [[userInfo objectForKey: AVAudioSessionInterruptionTypeKey] integerValue ];
180
+ if (typeValue == AVAudioSessionInterruptionTypeBegan) {
181
+ self.isInterrupt = YES ;
182
+ if ([self .audioPlayer isPlaying ]) {
183
+ [self .audioPlayer pause ];
184
+ }
185
+ } else {
186
+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue (), ^{
187
+ NSLog (@" playing" );
188
188
if (![self .audioPlayer isPlaying ]) {
189
189
[self .audioPlayer play ];
190
190
}
191
- }
191
+ self.isInterrupt = NO ;
192
+ });
192
193
}
193
194
}
194
195
}
0 commit comments