List available output audio target AVAudioSession

后端 未结 4 1599
慢半拍i
慢半拍i 2020-12-14 16:20

I need to list the audio outputs available to an iOS application. My question is similar to this one: How to list available audio output route on iOS

i tried this co

4条回答
  •  死守一世寂寞
    2020-12-14 16:46

    It will depend on your AVAudioSession category.

    You can safely assume on an iPhone that you have at least a microphone as input and a speaker as output. If you're trying to get a list of Bluetooth/AirPlay outputs, first you'd have to make sure your session category is reporting them to you:

    do 
    {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .AllowBluetooth)
        try audioSession.setActive(true)
    } 
    catch let e
    {
        debugPrint("failed to initialize audio session: \(e)")
    }
    

    Then a non-intuitive way to get available outputs is to check AVAudioSession.availableInputs as usually a bluetooth HFP device would have a mic too.. I might be assuming a lot right now.. but that's the only way to consistently get your availableOutputs.

    A better way is to use MultipleRoute category which will give you more freedom at accessing AVAudioSessionPort

提交回复
热议问题