Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio route changes #545

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,21 +823,24 @@ RNCallKeep.addEventListener('didToggleHoldCallAction', ({ hold, callUUID }) => {

});
```
- `hold` (boolean)
- `callUUID` (string)
- The UUID of the call.

### - didChangeAudioRoute

Triggered when the audio route has been changed.
⚠️ Will send `Speaker` on iOS but `SPEAKER` on Android.

```js
RNCallKeep.addEventListener('didChangeAudioRoute', ({ output }) => {
RNCallKeep.addEventListener('didChangeAudioRoute', ({ output,reason }) => {

});
```

- `hold` (boolean)
- `callUUID` (string)
- The UUID of the call.
- `output` (object)
- `name` (string)
- `type` (string)
- `reason` (int)

### - didPerformDTMFAction

Expand Down
5 changes: 1 addition & 4 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# CallKeep example



## How to install it

```
# Install dependancies
# Install dependencies
yarn install

cd ios
Expand All @@ -22,7 +20,6 @@ yarn start
yarn android # or yarn ios
```


## How this example was setted up

```sh
Expand Down
75 changes: 55 additions & 20 deletions ios/RNCallKeep/RNCallKeep.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ - (void)onAudioRouteChange:(NSNotification *)notification
{
NSDictionary *info = notification.userInfo;
NSInteger reason = [[info valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
NSString *output = [RNCallKeep getAudioOutput];
NSMutableDictionary *output = [RNCallKeep getAudioOutput];

if (output == nil) {
return;
Expand Down Expand Up @@ -160,8 +160,16 @@ + (void)initCallKitProvider {
}
}

+ (NSString *) getAudioOutput {
return [AVAudioSession sharedInstance].currentRoute.outputs.count > 0 ? [AVAudioSession sharedInstance].currentRoute.outputs[0].portType : nil;
+ (NSMutableDictionary *) getAudioOutput {
NSArray<AVAudioSessionPortDescription *> *outputs = [AVAudioSession sharedInstance].currentRoute.outputs;
if (outputs.count > 0) {
NSMutableDictionary *output = [[NSMutableDictionary alloc]init];
NSString * type = [RNCallKeep getAudioPortType: outputs[0].portType];
[output setObject:outputs[0].portName forKey:@"name"];
[output setObject:type forKey:@"type"];
return output;
}
return nil;
}

+ (void)setup:(NSDictionary *)options {
Expand Down Expand Up @@ -213,8 +221,8 @@ + (void)setup:(NSDictionary *)options {
#ifdef DEBUG
NSLog(@"[RNCallKeep][checkSpeaker]");
#endif
NSString *output = [RNCallKeep getAudioOutput];
resolve(@([output isEqualToString:@"Speaker"]));
NSMutableDictionary *output = [RNCallKeep getAudioOutput];
resolve(@([output[@"name"] isEqualToString:@"Speaker"]));
}

#pragma mark - CXCallController call actions
Expand Down Expand Up @@ -447,6 +455,21 @@ + (void)setup:(NSDictionary *)options {
}

NSArray *ports = [RNCallKeep getAudioInputs];

BOOL isCategorySetted = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
AVAudioSessionCategoryOptionAllowBluetoothA2DP | AVAudioSessionCategoryOptionAllowAirPlay error:&err];
if (!isCategorySetted)
{
NSLog(@"setCategory failed");
[NSException raise:@"setCategory failed" format:@"error: %@", err];
}
BOOL isCategoryActivated = [myAudioSession setActive:YES error:&err];
if (!isCategoryActivated)
{
NSLog(@"setActive failed");
[NSException raise:@"setActive failed" format:@"error: %@", err];
}

for (AVAudioSessionPortDescription *port in ports) {
if ([port.portName isEqualToString:inputName]) {
BOOL isSetted = [myAudioSession setPreferredInput:(AVAudioSessionPortDescription *)port error:&err];
Expand Down Expand Up @@ -496,7 +519,7 @@ + (NSMutableArray *) formatAudioInputs: (NSMutableArray *)inputs
NSLog(@"%@",str);
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:input.portName forKey:@"name"];
NSString * type = [RNCallKeep getAudioInputType: input.portType];
NSString * type = [RNCallKeep getAudioPortType: input.portType];
if(type)
{
[dict setObject:type forKey:@"type"];
Expand All @@ -509,18 +532,19 @@ + (NSMutableArray *) formatAudioInputs: (NSMutableArray *)inputs
+ (NSArray *) getAudioInputs
{
NSError* err = nil;
NSString *str = nil;

AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];

BOOL isCategorySetted = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&err];
if (!isCategorySetted)
{
NSLog(@"setCategory failed");
[NSException raise:@"setCategory failed" format:@"error: %@", err];
NSArray *calls = [RNCallKeep getCalls];
if (!calls || !calls.count){
BOOL isCategorySetted = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
AVAudioSessionCategoryOptionAllowBluetoothA2DP | AVAudioSessionCategoryOptionAllowAirPlay error:&err];
if (!isCategorySetted)
{
NSLog(@"setCategory failed");
[NSException raise:@"setCategory failed" format:@"error: %@", err];
}
}

BOOL isCategoryActivated = [myAudioSession setActive:YES error:&err];
BOOL isCategoryActivated = [myAudioSession setActive:TRUE error:&err];
if (!isCategoryActivated)
{
NSLog(@"setActive failed");
Expand All @@ -531,31 +555,41 @@ + (NSArray *) getAudioInputs
return inputs;
}

+ (NSString *) getAudioInputType: (NSString *) type
+ (NSString *) getAudioPortType: (NSString *) type
{
if ([type isEqualToString:AVAudioSessionPortAirPlay]){
return @"AirPlay";
}
if ([type isEqualToString:AVAudioSessionPortBuiltInReceiver]){
return @"Phone";
}
if ([type isEqualToString:AVAudioSessionPortBuiltInMic]){
return @"Phone";
}
else if ([type isEqualToString:AVAudioSessionPortHeadsetMic]){
return @"Headset";
return @"Headphones";
}
else if ([type isEqualToString:AVAudioSessionPortHeadphones]){
return @"Headset";
return @"Headphones";
}
else if ([type isEqualToString:AVAudioSessionPortBluetoothHFP]){
return @"Bluetooth";
}
else if ([type isEqualToString:AVAudioSessionPortBluetoothA2DP]){
return @"Bluetooth";
}
else if ([type isEqualToString:AVAudioSessionPortBluetoothLE]){
return @"Bluetooth";
}
else if ([type isEqualToString:AVAudioSessionPortBuiltInSpeaker]){
return @"Speaker";
}
else{
return nil;
return type;
}
}


- (void)requestTransaction:(CXTransaction *)transaction
{
#ifdef DEBUG
Expand Down Expand Up @@ -793,7 +827,8 @@ - (void)configureAudioSession
#endif

AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
AVAudioSessionCategoryOptionAllowBluetoothA2DP | AVAudioSessionCategoryOptionAllowAirPlay error:nil];

[audioSession setMode:AVAudioSessionModeDefault error:nil];

Expand Down
Loading