私のアプリでは、既に電話に接続されているAVAudioのポートタイプを確認したいと思います。以下のコードは、BluetoothA2DPとヘッドフォンで動作しますが、電話機を車のハンズフリーに接続するとBluetoothHFPでは動作しません。誰でも助けてくれますか?私はハンズフリー/ AV /ブルートゥース、および多くの他のすべてのSOの投稿をしていると思うが、それがBluetoothHFPの出力ポートの種類を認識していない理由を解決することはできません。アプリが見つかりませんAVAudio BluetoothHFPハンズフリーポートSwift
import AVFoundation
func startCheckAVConnection() {
// set the AVAudioSession to allow bluetooth. This do/try/catch doesn't seem to make a difference if it is here or not.
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth)
} catch{
print(error)
}
// Check possible outputs for handsfree
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
if outputs.count != 0 {
for output in outputs {
if output.portType == AVAudioSessionPortBluetoothA2DP {
peripheralLabel.text = "connected to BluetoothA2DP"
} else if output.portType == AVAudioSessionPortBluetoothHFP { // NOT RECOGNISED
peripheralLabel.text = "connected to BluetoothHFP"
} else if output.portType == AVAudioSessionPortHeadphones {
peripheralLabel.text = "connected to Headphones"
}
}
} else {
peripheralLabel.text = "Please connect handsfree"
}
// Add observer for audioRouteChangeListener
NotificationCenter.default.addObserver(
self,
selector: #selector(TopVC.audioRouteChangeListener(_:)),
name: NSNotification.Name.AVAudioSessionRouteChange,
object: nil)
}
「output.portType」にはどのような値がありますか?いくつの出力がありますか?あなたはいろんなことがあり、 'peripheralLabel.text'は別の値で上書きされますか? – Larme
こんにちは、A2DPまたはヘッドフォンをピックアップするときに.portTypeが1つしかありませんが、BluetoothHFPは決してピックアップしません。これ以上のアイデアは? – richc