强制 iPhone 麦克风作为音频输入

发布于 2024-09-28 09:49:26 字数 184 浏览 3 评论 0原文

我正在开发一个 iOS 应用程序(目前专门针对 iPhone),该应用程序需要仅从 iPhone 内部麦克风录制音频(即使插入耳机/耳麦),并在耳机上播放(我们假设现在已插入耳机)。

我想知道目前可用的 API 是否可以实现这一点?如果是这样,任何人都可以告诉我如何去做这件事吗?

谢谢!

I am developing an iOS application (targetted specifically for iPhone, at the moment) that requires the application to record audio only from iPhone internal microphone (even when headphone/headset is plugged in), and playback at headphone (let's assumed headphone is plugged in for now).

I am wondering if this is currently possible with the available APIs? If so, can anyone please shed some light on how do I go about doing this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

听风吹 2024-10-05 09:49:26

我相信这个问题的答案是“不”。我使用 iPhone 4 和新的 iOS 4 AVFoundation 进行实验,重点关注 AVCaptureDevice 类。

我将以下内容添加到应用程序中:

NSLog(@"%@", [AVCaptureDevice devices]);

因此,这要求列出可用于捕获音频和/或视频的所有设备。没有插入耳机时,我得到:

(
    "Back Camera",
    "Front Camera",
    "iPhone Microphone" 
)

插入耳机时,我得到:

(
    "Back Camera",
    "Front Camera",
    Headphones
)

因此,一旦耳机可用,iPhone 麦克风就会从可用 AVCaptureDevices 列表中消失。为了进一步探究这一点,我添加了一些快速代码来获取可用音频设备的 AVCaptureDevice 实例并打印其唯一 ID。对于将自身标识为“iPhone 麦克风”的设备和将自身标识为“耳机”的设备,我得到:

com.apple.avfoundation.avcapturedevice.built-in_audio:0

在我看来,两个设备显然不能具有相同的唯一 ID,因此显然它是同一台设备改变它的状态。尽管 AVCaptureDevices 有很多用于设置状态的内容,但它仅限于视觉内容,例如焦点、曝光、闪光和白平衡。

I believe the answer to this question to be 'no'. I used an iPhone 4 and the new-to-iOS 4 AVFoundation to experiment, focussing on the AVCaptureDevice class.

I added the following to an application:

NSLog(@"%@", [AVCaptureDevice devices]);

So that asks that all devices that can be used for capturing audio and/or video be listed. Without the headphones plugged in, I get:

(
    "Back Camera",
    "Front Camera",
    "iPhone Microphone" 
)

With the headphones plugged in I get:

(
    "Back Camera",
    "Front Camera",
    Headphones
)

So the iPhone microphone drops off the list of available AVCaptureDevices as soon as the headphones become available. To probe this further, I added a quick bit of code to grab the AVCaptureDevice instance for the available audio device and to print its unique ID. For both the device identifying itself as "iPhone Microphone" and the device identifying itself as "Headphones", I got:

com.apple.avfoundation.avcapturedevice.built-in_audio:0

It would seem to me to be obvious that two devices can't have the same unique ID, so clearly it's the same device changing its state. Although AVCaptureDevices have a lot of stuff for setting state, it's limited to visual things like focus, exposure, flash and white balance.

初见你 2024-10-05 09:49:26

看来确实是不可能了。

我的目标是将输出发送到蓝牙耳机并记录来自它的输入。
据我所知,我最好的选择是:“PlayAndRecord + AllowBluetoothInput”属性
(iphone 4,诺基亚 BH-214 耳机)

重要的是,根据苹果文档,当音频路由更改时,您始终必须重新覆盖音频类别!

这是我的 ROUTE CHANGE LISTENER 方法,打印:RouteChangeReasons、outputRoute、audioRout

void RouteChangeListener(void                    *inClientData,
                     AudioSessionPropertyID  inID,
                     UInt32                  inDataSize,
                     const void              *inData) {


if (inID == kAudioSessionProperty_AudioRouteChange) {

    NSLog(@"]-----------------[ Audio Route Change ]--------------------[");

    // ************************************************************************************************
    // Check route change reason **********************************************************************
    // ************************************************************************************************
    CFDictionaryRef routeDict = (CFDictionaryRef)inData;
    NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
    int reason = [reasonValue intValue];

    if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
        NSLog(@"] Logic: audio route change reason: OldDeviceUnavailable");
    }else if (reason == kAudioSessionRouteChangeReason_NewDeviceAvailable ) {
        NSLog(@"] Logic: audio route change reason: NewDeviceAvailable");
    }else if (reason == kAudioSessionRouteChangeReason_Unknown ) {
        NSLog(@"] Logic: audio route change reason: Unknown");
    }else if (reason == kAudioSessionRouteChangeReason_CategoryChange ) {
        NSLog(@"] Logic: audio route change reason: CategoryChange");
    }else if (reason == kAudioSessionRouteChangeReason_Override ) {
        NSLog(@"] Logic: audio route change reason: Override");
    }else if (reason == kAudioSessionRouteChangeReason_WakeFromSleep ) {
        NSLog(@"] Logic: audio route change reason: WakeFromSleep");
    }else if (reason == kAudioSessionRouteChangeReason_NoSuitableRouteForCategory ) {
        NSLog(@"] Logic: audio route chang reasone: NoSuitableRouteForCategory");
    }

    // ************************************************************************************************
    // Check output type ******************************************************************************
    // ************************************************************************************************
    CFDictionaryRef currentRouteDescriptionDictionary = nil;
    UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, ¤tRouteDescriptionDictionary);
    if (currentRouteDescriptionDictionary) {
        CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
        if(CFArrayGetCount(outputs) > 0) {
            CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
            CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);

            if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo) ) {                // if Airplay
                NSLog(@"] Logic: output changed to Airplay");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BluetoothA2DP, 0) == kCFCompareEqualTo) ) {     // if Bluetooth A2DP
                NSLog(@"] Logic: output changed to A2DP");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

                // Bluetooth support enable
                UInt32 allowBluetoothInput = 1;
                AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);


            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BluetoothHFP, 0) == kCFCompareEqualTo) ) {     // if Bluetooth HFP
                NSLog(@"] Logic: output changed to HFP");
                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

                // Bluetooth support enable
                UInt32 allowBluetoothInput = 1;
                AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_LineOut, 0) == kCFCompareEqualTo) ) {           // if Line Out
                NSLog(@"] Logic: output changed to Line Out");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_Headphones, 0) == kCFCompareEqualTo) ) {        // if Headphones
                NSLog(@"] Logic: output changed to Headphone");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

                // Bluetooth support disable
                UInt32 allowBluetoothInput = 0;
                AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);

            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BuiltInSpeaker, 0) == kCFCompareEqualTo) ) {    // if Built In Speaker
                NSLog(@"] Logic: output changed to Built In Speaker");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_USBAudio, 0) == kCFCompareEqualTo) ) {          // if USB audio
                NSLog(@"] Logic: output changed to USB Audio");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_HDMI, 0) == kCFCompareEqualTo) ) {              // if HDMI
                NSLog(@"] Logic: output changed to HDMI");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BuiltInReceiver, 0) == kCFCompareEqualTo) ) {   // if Built in Reciever
                NSLog(@"] Logic: output changed to Built in Reciever");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);
            }
            else {                                                                                                          // Unknown audio type
                NSLog(@"] Logic: WARNING: Unknown audio type: %@",(NSString*)outputType);
            }
        }



    }

    // ************************************************************************************************
    // Check audio route ******************************************************************************
    // ************************************************************************************************
    UInt32 routeSize = sizeof(CFStringRef);
    CFStringRef route;
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &routeSize, &route);
    NSLog(@"] Logic: the audio route is: %@",(NSString*)route);


    // ************************************************************************************************
    NSLog(@"]--------------------------[  ]-----------------------------[");        

}

}

Looks like its really not possible.

My goal is to send output to bluetooth headsets and record input from it too.
As far I can see, my best options are: "PlayAndRecord + AllowBluetoothInput" property
(iphone 4, nokia BH-214 headset)

IMPORTANT thing is that according to the apple documentation, you always have to RE-override your audio category when audio route changes!

THIS IS MY ROUTE CHANGE LISTENER method, that prints: RouteChangeReasons, outputRoute, audioRout:

void RouteChangeListener(void                    *inClientData,
                     AudioSessionPropertyID  inID,
                     UInt32                  inDataSize,
                     const void              *inData) {


if (inID == kAudioSessionProperty_AudioRouteChange) {

    NSLog(@"]-----------------[ Audio Route Change ]--------------------[");

    // ************************************************************************************************
    // Check route change reason **********************************************************************
    // ************************************************************************************************
    CFDictionaryRef routeDict = (CFDictionaryRef)inData;
    NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
    int reason = [reasonValue intValue];

    if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
        NSLog(@"] Logic: audio route change reason: OldDeviceUnavailable");
    }else if (reason == kAudioSessionRouteChangeReason_NewDeviceAvailable ) {
        NSLog(@"] Logic: audio route change reason: NewDeviceAvailable");
    }else if (reason == kAudioSessionRouteChangeReason_Unknown ) {
        NSLog(@"] Logic: audio route change reason: Unknown");
    }else if (reason == kAudioSessionRouteChangeReason_CategoryChange ) {
        NSLog(@"] Logic: audio route change reason: CategoryChange");
    }else if (reason == kAudioSessionRouteChangeReason_Override ) {
        NSLog(@"] Logic: audio route change reason: Override");
    }else if (reason == kAudioSessionRouteChangeReason_WakeFromSleep ) {
        NSLog(@"] Logic: audio route change reason: WakeFromSleep");
    }else if (reason == kAudioSessionRouteChangeReason_NoSuitableRouteForCategory ) {
        NSLog(@"] Logic: audio route chang reasone: NoSuitableRouteForCategory");
    }

    // ************************************************************************************************
    // Check output type ******************************************************************************
    // ************************************************************************************************
    CFDictionaryRef currentRouteDescriptionDictionary = nil;
    UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, ¤tRouteDescriptionDictionary);
    if (currentRouteDescriptionDictionary) {
        CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
        if(CFArrayGetCount(outputs) > 0) {
            CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
            CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);

            if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo) ) {                // if Airplay
                NSLog(@"] Logic: output changed to Airplay");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BluetoothA2DP, 0) == kCFCompareEqualTo) ) {     // if Bluetooth A2DP
                NSLog(@"] Logic: output changed to A2DP");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

                // Bluetooth support enable
                UInt32 allowBluetoothInput = 1;
                AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);


            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BluetoothHFP, 0) == kCFCompareEqualTo) ) {     // if Bluetooth HFP
                NSLog(@"] Logic: output changed to HFP");
                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

                // Bluetooth support enable
                UInt32 allowBluetoothInput = 1;
                AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_LineOut, 0) == kCFCompareEqualTo) ) {           // if Line Out
                NSLog(@"] Logic: output changed to Line Out");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_Headphones, 0) == kCFCompareEqualTo) ) {        // if Headphones
                NSLog(@"] Logic: output changed to Headphone");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

                // Bluetooth support disable
                UInt32 allowBluetoothInput = 0;
                AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);

            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BuiltInSpeaker, 0) == kCFCompareEqualTo) ) {    // if Built In Speaker
                NSLog(@"] Logic: output changed to Built In Speaker");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);

            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_USBAudio, 0) == kCFCompareEqualTo) ) {          // if USB audio
                NSLog(@"] Logic: output changed to USB Audio");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_HDMI, 0) == kCFCompareEqualTo) ) {              // if HDMI
                NSLog(@"] Logic: output changed to HDMI");
            }
            else if ( (CFStringCompare(outputType, kAudioSessionOutputRoute_BuiltInReceiver, 0) == kCFCompareEqualTo) ) {   // if Built in Reciever
                NSLog(@"] Logic: output changed to Built in Reciever");

                // Mix with others category
                UInt32 doSetProperty = 1;
                AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty);
            }
            else {                                                                                                          // Unknown audio type
                NSLog(@"] Logic: WARNING: Unknown audio type: %@",(NSString*)outputType);
            }
        }



    }

    // ************************************************************************************************
    // Check audio route ******************************************************************************
    // ************************************************************************************************
    UInt32 routeSize = sizeof(CFStringRef);
    CFStringRef route;
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &routeSize, &route);
    NSLog(@"] Logic: the audio route is: %@",(NSString*)route);


    // ************************************************************************************************
    NSLog(@"]--------------------------[  ]-----------------------------[");        

}

}

染年凉城似染瑾 2024-10-05 09:49:26

由于苹果从 7.0 开始再次更改了音频系统,我将在此处发布更新后的代码:

#pragma mark Route change listener
// *********************************************************************************************************
// *********** Route change listener ***********************************************************************
// *********************************************************************************************************
-(void)routeChanged:(NSNotification*)notification {

    NSLog(@"]-----------------[ Audio Route Change ]--------------------[");

    AVAudioSession *session = [AVAudioSession sharedInstance];

    //AVAudioSessionRouteDescription* prevRoute = [[notification userInfo] objectForKey:AVAudioSessionRouteChangePreviousRouteKey];

    // Reason
    NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    switch (reason) {
        case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
            NSLog(@"] Audio Route: The route changed because no suitable route is now available for the specified category.");
            break;
        case AVAudioSessionRouteChangeReasonWakeFromSleep:
            NSLog(@"] Audio Route: The route changed when the device woke up from sleep.");
            break;
        case AVAudioSessionRouteChangeReasonOverride:
            NSLog(@"] Audio Route: The output route was overridden by the app.");
            break;
        case AVAudioSessionRouteChangeReasonCategoryChange:
            NSLog(@"] Audio Route: The category of the session object changed.");
            break;
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
            NSLog(@"] Audio Route: The previous audio output path is no longer available.");
            break;
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            NSLog(@"] Audio Route: A preferred new audio output path is now available.");
            break;
        case AVAudioSessionRouteChangeReasonUnknown:
            NSLog(@"] Audio Route: The reason for the change is unknown.");
            break;
        default:
            NSLog(@"] Audio Route: The reason for the change is very unknown.");
            break;
    }

    // Output
    AVAudioSessionPortDescription *output = [[session.currentRoute.outputs count]?session.currentRoute.outputs:nil objectAtIndex:0];
    if ([output.portType isEqualToString:AVAudioSessionPortLineOut]) {
        NSLog(@"] Audio Route: Output Port: LineOut");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortHeadphones]) {
        NSLog(@"] Audio Route: Output Port: Headphones");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBluetoothA2DP]) {
        NSLog(@"] Audio Route: Output Port: BluetoothA2DP");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBuiltInReceiver]) {
        NSLog(@"] Audio Route: Output Port: BuiltInReceiver");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBuiltInSpeaker]) {
        NSLog(@"] Audio Route: Output Port: BuiltInSpeaker");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortHDMI]) {
        NSLog(@"] Audio Route: Output Port: HDMI");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortAirPlay]) {
        NSLog(@"] Audio Route: Output Port: AirPlay");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBluetoothLE]) {
        NSLog(@"] Audio Route: Output Port: BluetoothLE");
    }
    else {
        NSLog(@"] Audio Route: Output Port: Unknown: %@",output.portType);
    }

    // Input
    AVAudioSessionPortDescription *input = [[session.currentRoute.inputs count] ? session.currentRoute.inputs:nil objectAtIndex:0];

    if ([input.portType isEqualToString:AVAudioSessionPortLineIn]) {
        NSLog(@"] Audio Route: Input Port: LineIn");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
        NSLog(@"] Audio Route: Input Port: BuiltInMic");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortHeadsetMic]) {
        NSLog(@"] Audio Route: Input Port: HeadsetMic");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortBluetoothHFP]) {
        NSLog(@"] Audio Route: Input Port: BluetoothHFP");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortUSBAudio]) {
        NSLog(@"] Audio Route: Input Port: USBAudio");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortCarAudio]) {
        NSLog(@"] Audio Route: Input Port: CarAudio");
    }
    else {
        NSLog(@"] Audio Input Port: Unknown: %@",input.portType);
    }

    NSLog(@"]--------------------------[  ]-----------------------------[");

}

记住添加观察者,因为音频会话的委托也已弃用:

[[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(audioInterruption:)
                                                     name: AVAudioSessionInterruptionNotification
                                                   object: nil];

PS:您不需要在此处重置类别(如6.0)

Since apple changed the audio system again starting from 7.0 I'm going to post the update-ed code over here:

#pragma mark Route change listener
// *********************************************************************************************************
// *********** Route change listener ***********************************************************************
// *********************************************************************************************************
-(void)routeChanged:(NSNotification*)notification {

    NSLog(@"]-----------------[ Audio Route Change ]--------------------[");

    AVAudioSession *session = [AVAudioSession sharedInstance];

    //AVAudioSessionRouteDescription* prevRoute = [[notification userInfo] objectForKey:AVAudioSessionRouteChangePreviousRouteKey];

    // Reason
    NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    switch (reason) {
        case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
            NSLog(@"] Audio Route: The route changed because no suitable route is now available for the specified category.");
            break;
        case AVAudioSessionRouteChangeReasonWakeFromSleep:
            NSLog(@"] Audio Route: The route changed when the device woke up from sleep.");
            break;
        case AVAudioSessionRouteChangeReasonOverride:
            NSLog(@"] Audio Route: The output route was overridden by the app.");
            break;
        case AVAudioSessionRouteChangeReasonCategoryChange:
            NSLog(@"] Audio Route: The category of the session object changed.");
            break;
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
            NSLog(@"] Audio Route: The previous audio output path is no longer available.");
            break;
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            NSLog(@"] Audio Route: A preferred new audio output path is now available.");
            break;
        case AVAudioSessionRouteChangeReasonUnknown:
            NSLog(@"] Audio Route: The reason for the change is unknown.");
            break;
        default:
            NSLog(@"] Audio Route: The reason for the change is very unknown.");
            break;
    }

    // Output
    AVAudioSessionPortDescription *output = [[session.currentRoute.outputs count]?session.currentRoute.outputs:nil objectAtIndex:0];
    if ([output.portType isEqualToString:AVAudioSessionPortLineOut]) {
        NSLog(@"] Audio Route: Output Port: LineOut");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortHeadphones]) {
        NSLog(@"] Audio Route: Output Port: Headphones");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBluetoothA2DP]) {
        NSLog(@"] Audio Route: Output Port: BluetoothA2DP");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBuiltInReceiver]) {
        NSLog(@"] Audio Route: Output Port: BuiltInReceiver");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBuiltInSpeaker]) {
        NSLog(@"] Audio Route: Output Port: BuiltInSpeaker");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortHDMI]) {
        NSLog(@"] Audio Route: Output Port: HDMI");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortAirPlay]) {
        NSLog(@"] Audio Route: Output Port: AirPlay");
    }
    else if ([output.portType isEqualToString:AVAudioSessionPortBluetoothLE]) {
        NSLog(@"] Audio Route: Output Port: BluetoothLE");
    }
    else {
        NSLog(@"] Audio Route: Output Port: Unknown: %@",output.portType);
    }

    // Input
    AVAudioSessionPortDescription *input = [[session.currentRoute.inputs count] ? session.currentRoute.inputs:nil objectAtIndex:0];

    if ([input.portType isEqualToString:AVAudioSessionPortLineIn]) {
        NSLog(@"] Audio Route: Input Port: LineIn");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
        NSLog(@"] Audio Route: Input Port: BuiltInMic");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortHeadsetMic]) {
        NSLog(@"] Audio Route: Input Port: HeadsetMic");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortBluetoothHFP]) {
        NSLog(@"] Audio Route: Input Port: BluetoothHFP");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortUSBAudio]) {
        NSLog(@"] Audio Route: Input Port: USBAudio");
    }
    else if ([input.portType isEqualToString:AVAudioSessionPortCarAudio]) {
        NSLog(@"] Audio Route: Input Port: CarAudio");
    }
    else {
        NSLog(@"] Audio Input Port: Unknown: %@",input.portType);
    }

    NSLog(@"]--------------------------[  ]-----------------------------[");

}

Remember to add observer since the audio session's delegate is deprecated too:

[[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(audioInterruption:)
                                                     name: AVAudioSessionInterruptionNotification
                                                   object: nil];

PS: you dont need to reset the category here (as in 6.0)

叹沉浮 2024-10-05 09:49:26

这是不可能的,我尝试用路由更改的侦听器(使用 AudioSession)来弄清楚。我的结果是:您无法单独配置输入或输出,因为 Apple 提供的类别。我尝试*PlayAndRecord,当我配对蓝牙设备时,路由更改如下:

old route : HeadsetBT
new route : SpeakerAndMicrophone

事实上,我的蓝牙不是耳机,只是扬声器......所以对我来说没有解决方案。

It is not possible, I try to figure out with route changed listener (with AudioSession). My result is : you can't configure separately input or output because of categories provided by Apple. I try *PlayAndRecord, when I pair a bluetooth device, route change like this :

old route : HeadsetBT
new route : SpeakerAndMicrophone

In fact, my bluetooth is not an headset, just speakers... So for me there is no solution.

一紙繁鸢 2024-10-05 09:49:26

我非常有信心通过您的应用程序的音频会话可以实现这一点:

音频会话是中介
您的应用程序和 iOS 之间。每个
iPhone应用程序只有一个
音频会话。您将其配置为
表达您的应用程序的音频
意图。首先,你回答一些
关于你想要怎样的问题
应用程序的行为:

  • 您希望您的申请如何
    响应中断,例如
    打电话?
  • 您打算混合您的
    应用程序的声音来自
    其他正在运行的应用程序,或者你
    想让他们闭嘴吗?
  • 应该如何
    您的应用程序响应音频
    路线改变,例如,当用户
    插入或拔出耳机?


答案在手,你可以使用音频
会话接口(在
AudioToolbox/AudioServices.h) 到
配置您的音频会话和
应用程序。

深入研究这些文档:

和让我知道进展如何!

I am pretty confident this is possible via your application's Audio Session:

An audio session is an intermediary
between your application and iOS. Each
iPhone application has exactly one
audio session. You configure it to
express your application’s audio
intentions. To start, you answer some
questions about how you want your
application to behave:

  • How do you want your application to
    respond to interruptions, such as a
    phone call?
  • Do you intend to mix your
    application’s sounds with those from
    other running applications, or do you
    intend to silence them?
  • How should
    your application respond to an audio
    route change, for example, when a user
    plugs in or unplugs a headset?

With
answers in hand, you employ the audio
session interface (declared in
AudioToolbox/AudioServices.h) to
configure your audio session and your
application.

Dig on these docs:

And let me know how it goes!

前事休说 2024-10-05 09:49:26

我发现,当使用 AirPod pro 耳机并尝试录制语音邮件问候语时,即使耳机已完全连接并正在使用,手机也会使用内置麦克风。

I found that when using AirPod pro headphones and trying to record a voicemail greeting that the phone used the built in mic even though the headset was fully connected and in use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文