0 Replies
      Latest reply on Sep 25, 2015 3:27 PM by theanalogkid
      theanalogkid Apple Staff Apple Staff (610 points)

        When using the Remote I/O with the AVAudioSessionCategoryPlayAndRecord / AVAudioSessionCategoryOptionDefaultToSpeaker category/option combination, a number of developers have been interested in selecting the "Back" facing microphone instead of the default which seems to be the "Bottom" mic.

         

        Here's a straight forward snippet demonstrating how to do just that (a link to a Q&A with further information is provided at the bottom of this post):

         

            // activate the audio session
            [[AVAudioSession sharedInstance] setActive:YES error:&error];
            if (error) NSLog(@"ERROR SETTING SESSION ACTIVE! %ld", (long)error.code);
        
            // select the built-in Back mic if available
            NSArray *portDescriptions = sessionInstance.availableInputs;
            AVAudioSessionPortDescription* builtInMicPort = nil;
            AVAudioSessionDataSourceDescription* backDataSource = nil;
        
            NSLog(@"  availableInputs:\n");
            NSLog(@"%@", portDescriptions);
        
            for (AVAudioSessionPortDescription* port in portDescriptions) {
                if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
                    builtInMicPort = port;
                    break;
                }
            } // end input iteration
        
            NSLog(@"  port Info:\n");
            NSLog(@"%@", builtInMicPort.portType);
            NSLog(@"%@", builtInMicPort.portName);
            NSLog(@"%@", builtInMicPort.channels);
        
            NSLog(@"  dataSources:\n");
            NSLog(@"%@", builtInMicPort.dataSources);
            NSLog(@"%@", builtInMicPort.preferredDataSource);
            NSLog(@"%@", builtInMicPort.selectedDataSource);
        
            if (builtInMicPort) {
                for (AVAudioSessionDataSourceDescription* source in builtInMicPort.dataSources) {
                    if ([source.orientation isEqual:AVAudioSessionOrientationBack]) {
                        backDataSource = source;
                        break;
                    }
                } // end data source iteration
        
                if (backDataSource) {
                    NSError* theError = nil;
                    result = [builtInMicPort setPreferredDataSource:backDataSource error:&theError];
                    if (result) {
                        if (error) NSLog(@"ERROR SETTING PREFERRED DATA SOURCE! %ld", (long)error.code);
                    }
                }
            }
        
            NSLog(@"Current route:\n");
            NSLog(@"%@", [[AVAudioSession sharedInstance] currentRoute]);
        

         

        The above code selects the back facing mic (available in this case) and produces the following informational output:

         

        2015-09-25 15:00:42.118 yyz[965:1250704]   availableInputs:
        2015-09-25 15:00:42.118 yyz[965:1250704] (
            "<AVAudioSessionPortDescription: 0x170010c50, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Bottom>"
        )
        2015-09-25 15:00:42.119 yyz[965:1250704]   port Info:
        2015-09-25 15:00:42.119 yyz[965:1250704] MicrophoneBuiltIn
        2015-09-25 15:00:42.119 yyz[965:1250704] iPhone Microphone
        2015-09-25 15:00:42.119 yyz[965:1250704] (null)
        2015-09-25 15:00:42.119 yyz[965:1250704]   dataSources:
        2015-09-25 15:00:42.120 yyz[965:1250704] (
            "<AVAudioSessionDataSourceDescription: 0x170010cf0, ID = 1835216945; name = Bottom>",
            "<AVAudioSessionDataSourceDescription: 0x170010c30, ID = 1835216946; name = Front>",
            "<AVAudioSessionDataSourceDescription: 0x170014370, ID = 1835216947; name = Back>"
        )
        2015-09-25 15:00:42.123 yyz[965:1250704] (null)
        2015-09-25 15:00:42.124 yyz[965:1250704] <AVAudioSessionDataSourceDescription: 0x170010cf0, ID = 1835216945; name = Bottom>
        2015-09-25 15:00:42.233 yyz[965:1250704] Current route:
        2015-09-25 15:00:42.236 yyz[965:1250704] <AVAudioSessionRouteDescription: 0x17400e790,
        inputs = (
            "<AVAudioSessionPortDescription: 0x17400e740, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Back>"
        

         

        Further details can be found in Q&A 1799 < https://developer.apple.com/library/ios/qa/qa1799/_index.html  >