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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// IBTAudioController.m
// XFFruit
//
// Created by Xummer on 4/17/15.
// Copyright (c) 2015 Xummer. All rights reserved.
//
#import "IBTAudioController.h"
@import AVFoundation;
@import AudioToolbox;
@interface IBTAudioController () <AVAudioSessionDelegate>
@end
@implementation IBTAudioController
static IBTAudioController *sharedInstance = nil;
+ (IBTAudioController *)sharedController {
@synchronized(self) {
if (nil == sharedInstance) {
sharedInstance = [[self alloc] init];
}
}
return sharedInstance;
}
+ (void)unload {
if (sharedInstance) {
sharedInstance = nil;
}
}
- (id)init{
self = [super init];
if (self) {
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
[notiCenter addObserver: self
selector: @selector(proximityStateDidChange:)
name: UIDeviceProximityStateDidChangeNotification
object: nil];
}
return self;
}
- (void)dealloc {
// AVAudioSession *mySession = [AVAudioSession sharedInstance];
// [mySession setDelegate:nil];
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
[notiCenter removeObserver: self
name: AVAudioSessionInterruptionNotification
object: nil];
[notiCenter removeObserver: self
name: AVAudioSessionRouteChangeNotification
object: nil];
[self deactivate];
[notiCenter removeObserver: self
name: UIDeviceProximityStateDidChangeNotification
object: nil];
}
//the callback define for audio route change
void propChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,
const void *inPropertyValue
);
- (void)setup {
AVAudioSession *mySession = [AVAudioSession sharedInstance];
// Assign the Playback category to the audio session.
// OSStatus err;
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
[notiCenter addObserver: self
selector: @selector(handleInterruption:)
name: AVAudioSessionInterruptionNotification
object: mySession];
[notiCenter addObserver: self
selector: @selector(handleRouteChange:)
name: AVAudioSessionRouteChangeNotification
object: nil];
// // listen for audio route changes
// err = AudioSessionAddPropertyListener(
// kAudioSessionProperty_AudioRouteChange,
// propChangeListenerCallback,
// self);
// if(kAudioSessionNoError != err){
// NSLog(@"unable to register property listener for AudioRouteChange.");
// return NO;
// }
//
// // listen for audio input availability changes
// err = AudioSessionAddPropertyListener(
// kAudioSessionProperty_AudioInputAvailable,
// propChangeListenerCallback,
// self);
// if(kAudioSessionNoError != err){
// NSLog(@"unable to register property listener for AudioInputAvailable.");
// return NO;
// }
// if(![self activate]){
// NSLog(@"unable to activate AudioSession.");
// return NO;
// }
}
- (BOOL)setAVAudioSessionCategory:(NSString*)category
{
AVAudioSession *mySession = [AVAudioSession sharedInstance];
// Assign the Playback category to the audio session.
NSError *audioSessionError = nil;
[mySession setCategory: category
error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (@"Error setting audio session category.");
return NO;
}
else {
return YES;
}
}
- (BOOL)switchToPlaybackMode {
return [self setAVAudioSessionCategory:AVAudioSessionCategoryPlayback];
}
- (BOOL)switchToPlayAndRecordMode {
return [self setAVAudioSessionCategory:AVAudioSessionCategoryPlayAndRecord];
}
- (void)reset {
}
- (BOOL)activate {
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),
&sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride);
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//默认情况下扬声器播放
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *audioSessionError = nil;
[audioSession setActive:YES error:&audioSessionError];
if (audioSessionError != nil) {
NSLog (@"Error activating audio session during initial setup.");
return NO;
}
else {
return YES;
}
}
- (BOOL)deactivate {
AVAudioSession *mySession = [AVAudioSession sharedInstance];
NSError *audioSessionError = nil;
// Deactivate the audio session
[mySession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&audioSessionError];
if (audioSessionError != nil) {
NSLog (@"Error activating audio session during initial setup.");
return NO;
}
else {
return YES;
}
}
#pragma mark -
#pragma mark -
- (void)handleInterruption:(NSNotification*)notification{
NSInteger reason = 0;
NSString* reasonStr= @"";
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
//Posted when an audio interruption occurs.
reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
if (reason == AVAudioSessionInterruptionTypeBegan) {
// Audio has stopped, already inactive
// Change state of UI, etc., to reflect non-playing state
// if(soundSessionIO_.isProcessingSound)[soundSessionIO_ stopSoundProcessing:nil];
}
if (reason == AVAudioSessionInterruptionTypeEnded) {
// Make session active
// Update user interface
// AVAudioSessionInterruptionOptionShouldResume option
reasonStr = @"AVAudioSessionInterruptionTypeEnded";
NSNumber* seccondReason = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey] ;
switch ([seccondReason integerValue]) {
case AVAudioSessionInterruptionOptionShouldResume:
// Indicates that the audio session is active and immediately ready to be used. Your app can resume the audio operation that was interrupted.
break;
default:
break;
}
}
if ([notification.name isEqualToString:@"AVAudioSessionDidBeginInterruptionNotification"]) {
// if (soundSessionIO_.isProcessingSound) {
//
// }
// Posted after an interruption in your audio session occurs.
// This notification is posted on the main thread of your app. There is no userInfo dictionary.
}
if ([notification.name isEqualToString:@"AVAudioSessionDidEndInterruptionNotification"]) {
// Posted after an interruption in your audio session ends.
// This notification is posted on the main thread of your app. There is no userInfo dictionary.
}
if ([notification.name isEqualToString:@"AVAudioSessionInputDidBecomeAvailableNotification"]) {
// Posted when an input to the audio session becomes available.
// This notification is posted on the main thread of your app. There is no userInfo dictionary.
}
if ([notification.name isEqualToString:@"AVAudioSessionInputDidBecomeUnavailableNotification"]) {
// Posted when an input to the audio session becomes unavailable.
// This notification is posted on the main thread of your app. There is no userInfo dictionary.
}
};
NSLog(@"handleInterruption: %@ reason %@",[notification name],reasonStr);
}
#pragma mark -
#pragma mark -
- (void)handleRouteChange:(NSNotification*)notification{
AVAudioSession *session = [ AVAudioSession sharedInstance ];
NSString* seccReason = @"";
NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
// AVAudioSessionRouteDescription* prevRoute = [[notification userInfo] objectForKey:AVAudioSessionRouteChangePreviousRouteKey];
switch (reason) {
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
seccReason = @"The route changed because no suitable route is now available for the specified category.";
break;
case AVAudioSessionRouteChangeReasonWakeFromSleep:
seccReason = @"The route changed when the device woke up from sleep.";
break;
case AVAudioSessionRouteChangeReasonOverride:
seccReason = @"The output route was overridden by the app.";
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
seccReason = @"The category of the session object changed.";
UInt32 newCategory;
UInt32 size = sizeof(newCategory);
if (AudioSessionGetProperty(kAudioSessionProperty_AudioCategory, &size, &newCategory) == noErr &&
newCategory != kAudioSessionCategory_PlayAndRecord && newCategory != kAudioSessionCategory_RecordAudio) {
}
NSLog(@"Audio category %u %d %d %d %d %d %d\n", (unsigned int)newCategory, kAudioSessionCategory_AmbientSound,
kAudioSessionCategory_SoloAmbientSound, kAudioSessionCategory_MediaPlayback, kAudioSessionCategory_RecordAudio, kAudioSessionCategory_PlayAndRecord
, kAudioSessionCategory_AudioProcessing);
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
seccReason = @"The previous audio output path is no longer available.";
break;
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
seccReason = @"A preferred new audio output path is now available.";
break;
case AVAudioSessionRouteChangeReasonUnknown:
default:
seccReason = @"The reason for the change is unknown.";
break;
}
AVAudioSessionPortDescription *input = [[session.currentRoute.inputs count]?session.currentRoute.inputs:nil objectAtIndex:0];
if (input.portType == AVAudioSessionPortHeadsetMic) {
}
}
#pragma mark -
#pragma mark UIDeviceProximityStateDidChangeNotification
- (void)proximityStateDidChange:(NSNotification *)notification;
{
UIDevice *device = [notification object];
if ([device proximityState]){
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}
else{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}
#pragma mark -
#pragma mark Audio Route Change Listener
// Audio session callback function for responding to audio route changes. If playing back audio and
// the user unplugs a headset or headphones, or removes the device from a dock connector for hardware
// that supports audio playback, this callback detects that and stops playback.
//
// Refer to AudioSessionPropertyListener in Audio Session Services Reference.
void propChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,
const void *inPropertyValue
) {
// AudioController *controller = (AudioController*) inUserData;
// Ensure that this callback was invoked because of an audio route change
if (inPropertyID == kAudioSessionProperty_AudioRouteChange) {
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary,
CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue(routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
NSLog(@"kAudioSessionRouteChangeReason_OldDeviceUnavailable.\n");
//occurs when headset is unplugged
}
else if(routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {
//occurs when headset is plugged
NSLog(@"kAudioSessionRouteChangeReason_NewDeviceAvailable.\n");
}
else if (routeChangeReason == kAudioSessionRouteChangeReason_NoSuitableRouteForCategory) {
NSLog(@"kAudioSessionRouteChangeReason_NoSuitableRouteForCategory.\n");
}
else if(routeChangeReason == kAudioSessionRouteChangeReason_CategoryChange) {
NSLog(@"kAudioSessionRouteChangeReason_CategoryChange.\n");
UInt32 newCategory;
UInt32 size = sizeof(newCategory);
if (AudioSessionGetProperty(kAudioSessionProperty_AudioCategory, &size, &newCategory) == noErr &&
newCategory != kAudioSessionCategory_PlayAndRecord && newCategory != kAudioSessionCategory_RecordAudio) {
}
NSLog(@"Audio category %u %d %d %d %d %d %d\n", (unsigned int)newCategory, kAudioSessionCategory_AmbientSound,
kAudioSessionCategory_SoloAmbientSound, kAudioSessionCategory_MediaPlayback, kAudioSessionCategory_RecordAudio, kAudioSessionCategory_PlayAndRecord
, kAudioSessionCategory_AudioProcessing);
}
}
else if(inPropertyID == kAudioSessionProperty_AudioInputAvailable) {
//may resume the audio record if the input become available
}
}
@end