Skip to content

Commit e2d571d

Browse files
committed
set category and mode with a single call + add more debug info -> reason & interruption type
1 parent 8b0a6ae commit e2d571d

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

Assets/Samples/Stream Video & Audio Chat SDK/0.7.0/Video & Audio Chat Example Project/Plugins/iOS/AudioSessionMonitor.mm

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ - (void)prepareAudioSessionForRecording {
4545
NSLog(@"[Stream] Add option AVAudioSessionCategoryOptionAllowBluetoothA2DP");
4646
}
4747

48-
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:&error];
48+
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVideoChat options:options error:&error];
4949

5050
if(!success){
5151
NSLog(@"[Stream] Error setting audio session category: %@", error.localizedDescription);
@@ -54,7 +54,7 @@ - (void)prepareAudioSessionForRecording {
5454
}
5555

5656
// Set audio session mode
57-
if(@available(iOS 5.0, *)){
57+
if(@available(iOS 5.0, *) && false){ // TEMP DISABLE for testing. perhaps not needed at all, the setCategory has mode
5858
success = [audioSession setMode:AVAudioSessionModeVideoChat error:&error];
5959

6060
if(!success){
@@ -184,9 +184,16 @@ - (void)registerNotifications{
184184
NSDictionary *settings = [self getCurrentAudioSettings];
185185
NSLog(@"[Stream] Audio Route Changed: %@", settings);
186186

187+
NSNumber *reasonValue = notification.userInfo[AVAudioSessionRouteChangeReasonKey];
188+
AVAudioSessionRouteChangeReason reason = (AVAudioSessionRouteChangeReason)[reasonValue unsignedIntegerValue];
189+
190+
// Map reason to a string
191+
NSString *reasonLabel = [self getLabelForRouteChangeReason:reason];
192+
187193
NSDictionary *eventInfo = @{
188194
@"type": @"routeChange",
189-
@"settings": settings
195+
@"settings": settings,
196+
@"reason": reasonLabel
190197
};
191198

192199
// To JSON and send to Unity
@@ -200,10 +207,17 @@ - (void)registerNotifications{
200207

201208
[center addObserverForName:AVAudioSessionInterruptionNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
202209
NSInteger type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] integerValue];
203-
NSLog(@"[Stream] Audio Session Interrupted: %ld", (long)type);
210+
NSString *label = [self labelForInterruptionType:(AVAudioSessionInterruptionType)type];
211+
212+
NSNumber *reasonValue = notification.userInfo[AVAudioSessionInterruptionReasonKey];
213+
NSString *reasonLabel = reasonValue ? [self labelForInterruptionReason:(AVAudioSessionInterruptionReason)[reasonValue integerValue]] : @"Unknown";
214+
215+
NSLog(@"[Stream] Audio Session Interrupted: %@, Reason: %@", label, reasonLabel);
204216

205217
NSDictionary *eventInfo = @{
206218
@"type": @"interruption",
219+
@"interruptionType": label,
220+
@"reason": reasonLabel
207221
};
208222

209223
// To JSON and send to Unity
@@ -236,6 +250,52 @@ - (void)unregisterNotifications {
236250
[self.notificationCenter removeObserver:self];
237251
}
238252

253+
- (NSString *)getLabelForRouteChangeReason:(AVAudioSessionRouteChangeReason)reason {
254+
switch (reason) {
255+
case AVAudioSessionRouteChangeReasonUnknown:
256+
return @"Unknown";
257+
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
258+
return @"NewDeviceAvailable";
259+
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
260+
return @"OldDeviceUnavailable";
261+
case AVAudioSessionRouteChangeReasonCategoryChange:
262+
return @"CategoryChange";
263+
case AVAudioSessionRouteChangeReasonOverride:
264+
return @"Override";
265+
case AVAudioSessionRouteChangeReasonWakeFromSleep:
266+
return @"WakeFromSleep";
267+
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
268+
return @"NoSuitableRouteForCategory";
269+
case AVAudioSessionRouteChangeReasonRouteConfigurationChange:
270+
return @"RouteConfigurationChange";
271+
default:
272+
return @"Unknown";
273+
}
274+
}
275+
276+
- (NSString *)labelForInterruptionType:(AVAudioSessionInterruptionType)type {
277+
switch (type) {
278+
case AVAudioSessionInterruptionTypeBegan:
279+
return @"Began";
280+
case AVAudioSessionInterruptionTypeEnded:
281+
return @"Ended";
282+
default:
283+
return @"Unknown";
284+
}
285+
}
286+
287+
- (NSString *)labelForInterruptionReason:(AVAudioSessionInterruptionReason)reason {
288+
switch (reason) {
289+
case AVAudioSessionInterruptionReasonAppWasSuspended:
290+
return @"AppWasSuspended";
291+
case AVAudioSessionInterruptionReasonBuiltInMicMuted:
292+
return @"BuiltInMicMuted";
293+
default:
294+
return @"Unknown";
295+
}
296+
}
297+
298+
239299
@end
240300

241301

Assets/Samples/Stream Video & Audio Chat SDK/0.7.0/Video & Audio Chat Example Project/Scripts/AudioSessionMonitor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private void OnAudioSessionEvent(string jsonData)
114114
case "interruption":
115115
var interruptEvent = new AudioInterruptionEventData
116116
{
117+
Reason = eventData["reason"].ToString(),
117118
InterruptionType = Convert.ToInt32(eventData["interruptionType"]),
118119
Settings = ParseAudioSettings(eventData["settings"])
119120
};
@@ -280,6 +281,7 @@ public class AudioRouteChangeEventData
280281

281282
public class AudioInterruptionEventData
282283
{
284+
public string Reason { get; set; }
283285
public int InterruptionType { get; set; }
284286
public AudioSettings Settings { get; set; }
285287
}

0 commit comments

Comments
 (0)