车载音频通道切换至蓝牙,如何捕获 MPVolumeView 的通知?

解读

在国内车载项目面试中,面试官真正想确认的是:

  1. 你是否知道 iOS 的音频路由切换不是靠 MPVolumeView 本身“通知”,而是靠系统级通知中心;
  2. 你是否能在 Objective-C 层面正确注册、筛选并解析蓝牙相关的路由变化;
  3. 你是否了解车载场景下的“隐私权限+后台模式”双重要求,否则即使代码写对,实车也收不到通知。

一句话:问的是“MPVolumeView 相关”的通知,答的必须是“系统音频路由通知”,并给出可落地的 Objective-C 代码与实车验证要点。

知识点

  1. MPVolumeView 只是 UI 控件,本身不发送 NSNotification;真正的路由切换通知由 AVAudioSession 发出,名称为 AVAudioSessionRouteChangeNotification。
  2. 车载蓝牙属于 AVAudioSessionPortBluetoothA2DP(或旧称 BluetoothA2DP),需通过 currentRoute.outputs 遍历比对 portType。
  3. 注册通知前必须配置 AVAudioSession:
    • 设置 Category 为 AVAudioSessionCategoryPlayback(车载多为播放场景);
    • 激活 Session;
    • 在 Info.plist 里声明 Required Background Modes → audio,否则灭屏后通知会断。
  4. 路由变化 reason 枚举中,AVAudioSessionRouteChangeReasonNewDeviceAvailable 表示新设备(蓝牙)插入,AVAudioSessionRouteChangeReasonOldDeviceUnavailable 表示拔出。
  5. 国内车机项目还需注意 CarPlay 共存场景:当 CarPlay 激活时,系统会把默认输出指向 CarPlay,而不是本地蓝牙,需额外判断 portName 是否包含 “CarPlay” 或 “USB”。

答案

// AppDelegate 或单例初始化
- (void)setupAudioRouteObserver {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error = nil;

    // 1. 配置 Category 并激活
    [session setCategory:AVAudioSessionCategoryPlayback
                   error:&error];
    if (error) {
        NSLog(@"Category 设置失败: %@", error.localizedDescription);
        return;
    }
    [session setActive:YES error:&error];
    if (error) {
        NSLog(@"Session 激活失败: %@", error.localizedDescription);
        return;
    }

    // 2. 注册路由变化通知
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(handleRouteChange:)
     name:AVAudioSessionRouteChangeNotification
     object:session];
}

// 3. 通知回调
- (void)handleRouteChange:(NSNotification *)notification {
    NSInteger reason = [notification.userInfo[AVAudioSessionRouteChangeReasonKey] integerValue];

    if (reason == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {
        AVAudioSessionRouteDescription *route = [[AVAudioSession sharedInstance] currentRoute];
        for (AVAudioSessionPortDescription *output in route.outputs) {
            if ([output.portType isEqualToString:AVAudioSessionPortBluetoothA2DP]) {
                NSLog(@"车载蓝牙已连接,通道切换至:%@", output.portName);
                // 此处可刷新 UI 或通知业务层
                break;
            }
        }
    } else if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
        NSLog(@"蓝牙断开,切回扬声器");
    }
}

// 4. 反注册
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:AVAudioSessionRouteChangeNotification
                                              object:[AVAudioSession sharedInstance]];
}

关键点总结:

  • 代码里完全没有 MPVolumeView,因为控件本身不抛通知;
  • 用 currentRoute.outputs 遍历比对 portType,精准识别蓝牙;
  • 车载项目务必在 Info.plist 加 audio 后台模式,否则灭屏即失效。

拓展思考

  1. 如果车机同时支持蓝牙电话(HFP)与蓝牙媒体(A2DP),需要再监听 AVAudioSessionPortBluetoothHFP,并在 UI 上区分“通话声道”与“媒体声道”。
  2. 国内部分后装车机通过 USB 声卡实现音频输入输出,此时 portType 为 AVAudioSessionPortUSBAudio,需要与蓝牙做优先级仲裁。
  3. 当用户通过 MPVolumeView 的 AirPlay 按钮手动切换至 iPhone 扬声器,系统同样会发送 RouteChangeNotification,但 reason 为 AVAudioSessionRouteChangeReasonOverride,需提示用户“已手动切换,如需自动回连蓝牙请重新插拔”。
  4. 若项目支持 CarPlay,需额外监听 MPVolumeView 的 WirelessRouteActiveDidChangeNotification(私有通知,可动态链接 private framework 后使用),以捕获无线 CarPlay 激活事件,避免与蓝牙 A2DP 冲突。
  5. 面试时可以反问面试官:“贵司车机是否集成 CarPlay?是否需要在 Route 切换时同步更新 HMI 的音量条?” 体现你对整车音频策略的理解深度。