10000 [iOS] Fixed bug for enableSpeakerPhone. · next-coder/flutter-webrtc@bdda600 · GitHub
[go: up one dir, main page]

Skip to content

Commit bdda600

Browse files
committed
[iOS] Fixed bug for enableSpeakerPhone.
1 parent 97972c8 commit bdda600

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

ios/Classes/FlutterWebRTCPlugin.m

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ @implementation FlutterWebRTCPlugin {
1414
id _registry;
1515
id _messenger;
1616
id _textures;
17+
BOOL _speakerOn;
1718
}
1819

1920
@synthesize messenger = _messenger;
@@ -45,6 +46,7 @@ - (instancetype)initWithChannel:(FlutterMethodChannel *)channel
4546
_registry = registrar;
4647
_textures = textures;
4748
_messenger = messenger;
49+
_speakerOn = NO;
4850
self.viewController = viewController;
4951
}
5052

@@ -60,11 +62,31 @@ - (instancetype)initWithChannel:(FlutterMethodChannel *)channel
6062
self.localStreams = [NSMutableDictionary new];
6163
self.localTracks = [NSMutableDictionary new];
6264
self.renders = [[NSMutableDictionary alloc] init];
65+
66+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil];
67+
6368
return self;
6469
}
6570

71+
72+
- (void)didSessionRouteChange:(NSNotification *)notification {
73+
NSDictionary *interuptionDict = notification.userInfo;
74+
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
75+
76+
switch (routeChangeReason) {
77+
case AVAudioSessionRouteChangeReasonCategoryChange: {
78+
NSError* error;
79+
[[AVAudioSession sharedInstance] overrideOutputAudioPort:_speakerOn? AVAudioSessionPortOverrideSpeaker : AVAudioSessionPortOverrideNone error:&error];
80+
}
81+
break;
82+
83+
default:
84+
break;
85+
}
86+
}
87+
6688
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result {
67-
89+
6890
if ([@"createPeerConnection" isEqualToString:call.method]) {
6991
NSDictionary* argsMap = call.arguments;
7092
NSDictionary* configuration = argsMap[@"configuration"];
@@ -466,9 +488,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result
466488
} else if ([@"enableSpeakerphone" isEqualToString:call.method]) {
467489
NSDictionary* argsMap = call.arguments;
468490
NSNumber* enable = argsMap[@"enable"];
491+
_speakerOn = enable.boolValue;
469492
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
470493
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
471-
withOptions:enable.boolValue ? AVAudioSessionCategoryOptionDefaultToSpeaker : 0
494+
withOptions:_speakerOn ? AVAudioSessionCategoryOptionDefaultToSpeaker : 0
472495
error:nil];
473496
[audioSession setActive:YES error:nil];
474497
result(nil);

0 commit comments

Comments
 (0)
0