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

Cannot get Receiver to be selected audio device #213

Open
eharmshoagie opened this issue Oct 16, 2024 · 0 comments
Open

Cannot get Receiver to be selected audio device #213

eharmshoagie opened this issue Oct 16, 2024 · 0 comments

Comments

@eharmshoagie
Copy link

Using the following code, I cannot set the audio output to Receiver. I can get the Bluetooth and Speaker to be selected, and Bluetooth is always the priority.

Is there something I am doing wrong that is preventing the Receiver to be chosen? I can verify the Selected deviceType matches the strings in the if statement.

When selecting Handset, I get the" Successfully switched to handset (earpiece)" NSLog but Speaker gets selected

Your demo app always selects Speaker.

I am using pod 'AmazonChimeSDK', '0.26.1'

RCT_EXPORT_METHOD(setAudioOutput:(NSDictionary *)outputDevice
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
    // Extract the deviceType from the outputDevice dictionary
    NSString *deviceType = outputDevice[@"type"];
    NSLog(@"Selected deviceType: %@", deviceType);
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error = nil;

    // Set the AVAudioSession category to handle both input and output devices.
    BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                            withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                                  error:&error];
    if (!success || error) {
        reject(@"session_error", @"Failed to set audio session category", error);
        return;
    }

    // Activate the audio session to apply changes
    NSError *activationError = nil;
    BOOL isSessionActivated = [session setActive:YES error:&activationError];
    if (!isSessionActivated || activationError) {
        NSLog(@"Failed to activate audio session: %@", activationError);
        reject(@"session_activation_error", @"Failed to activate audio session", activationError);
        return;
    }

    // Handle switching output based on deviceType
    if ([deviceType isEqualToString:@"BUILTIN_SPEAKER"]) {
        // Set output to the built-in speaker
        success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
        if (success) {
            NSLog(@"Successfully switched to built-in speaker");
            resolve(@{@"selected": @"built-in speaker"});
        } else {
            NSLog(@"Error switching to built-in speaker: %@", error);
            reject(@"output_error", @"Failed to set speaker as audio output", error);
        }
    } 
    else if ([deviceType isEqualToString:@"BLUETOOTH"]) {
        // Set the audio session to allow Bluetooth input/output
        success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                            withOptions:AVAudioSessionCategoryOptionAllowBluetooth
                                  error:&error];
        if (success) {
            NSLog(@"Successfully switched to Bluetooth");
            resolve(@{@"selected": @"bluetooth"});
        } else {
            NSLog(@"Error switching to Bluetooth: %@", error);
            reject(@"output_error", @"Failed to set Bluetooth audio output", error);
        }
    } 
    else {
        // Explicitly select the handset (earpiece) without overriding the default to Bluetooth
        NSArray<AVAudioSessionPortDescription *> *availableOutputs = session.currentRoute.outputs;
        AVAudioSessionPortDescription *handsetPort = nil;
        
        // Find the correct handset port (the earpiece or receiver)
        for (AVAudioSessionPortDescription *port in availableOutputs) {
            if ([port.portType isEqualToString:AVAudioSessionPortBuiltInReceiver]) {
                handsetPort = port;
                break;
            }
        }

        if (handsetPort) {
            // Explicitly set the earpiece (handset) as the output
            success = [session setPreferredInput:handsetPort error:&error];
            if (success) {
                NSLog(@"Successfully switched to handset (earpiece)");
                resolve(@{@"selected": @"handset"});
            } else {
                NSLog(@"Error switching to handset: %@", error);
                reject(@"output_error", @"Failed to set handset as audio output", error);
            }
        } else {
            NSLog(@"Handset not found.");
            reject(@"output_error", @"Handset not found in available outputs", nil);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant