通話時の音声出力(iOS)

Applications内でシームレスに音声出力を切り替えるには、Appleの堅牢なCallKitフレームワークを統合することをお勧めします。CallKitを利用することで、アプリは通話サービスを効率的に管理でき、スムーズなユーザー体験を確保できます。CallKitに関するAppleの公式ドキュメントをご参照ください。 これ.

CallKitの統合

CallKitをプロジェクトに組み込み、その機能を最大限に活用するための包括的なガイドについては、以下をご参照ください。 プッシュ通知.

現在のオーディオ出力経路

現在利用可能なオーディオ出力ソースを取得するには、以下を利用できます。 AVAudioSession's currentRoute.outputs プロパティ。これにより、現時点で利用可能なすべての出力ソースの一覧が表示されます。

let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
outputs.forEach { audioSource in
                switch audioSource.portType {
                case .headphones:
                    print("headphones are used")
                case .bluetoothA2DP:
                    print("Bluetooth audio is connected")
                case .builtInSpeaker:
                    print("speaker is used")
                case .builtInReceiver:
                    print("earpiece is used")
                default:
                    print("other output: \(audioSource.portType.rawValue)")
                }
            }

また、次のコードスニペットを使用して、オーディオ出力ソースを上書きすることもできます:

// highlight-next-line
AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)

以下の方法によるオーディオ設定の設定や上書きは避けることをお勧めします。 コールキット より快適な体験のために。

オーディオ出力の変化の確認

を購読することができる。 AVAudioSession.routeChangeNotification オーディオ出力の変化を確認するための通知です。その方法は以下の通りです:

var cancelable: AnyCancellable?

func observeAudioRouteChanges() {
    self.cancelable = NotificationCenter.default.publisher(for: AVAudioSession.routeChangeNotification).sink { notification in
            /// process notification's user Info, to get audio change information
    }
}

func removeAudioRouteChangesObserver() {
    cancelable?.cancel()
}