Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios A音频会话未检测到已连接的蓝牙设备_Ios_Iphone_Swift_Bluetooth_Avaudioplayer - Fatal编程技术网

Ios A音频会话未检测到已连接的蓝牙设备

Ios A音频会话未检测到已连接的蓝牙设备,ios,iphone,swift,bluetooth,avaudioplayer,Ios,Iphone,Swift,Bluetooth,Avaudioplayer,我正在使用以下代码检测可用的输出,但它没有拾取我连接的蓝牙设备。我知道我的蓝牙设备已正确连接,因为我正在播放来自它的音乐 let session = AVAudioSession.sharedInstance() for description in session.currentRoute.outputs { if description.portType == AVAudioSessionPortHeadphones {

我正在使用以下代码检测可用的输出,但它没有拾取我连接的蓝牙设备。我知道我的蓝牙设备已正确连接,因为我正在播放来自它的音乐

let session = AVAudioSession.sharedInstance()
        for description in session.currentRoute.outputs {
            if description.portType == AVAudioSessionPortHeadphones {
                NSLog("headphone plugged in")
                let alertActin = UIAlertAction(title: "Headphones", style: UIAlertActionStyle.default, handler:nil)
                alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(alertActin)
            } else if description.portType == AVAudioSessionPortBluetoothA2DP {
                NSLog("Bluetooth plugged in")

                let alertActin = UIAlertAction(title: description.portName, style: UIAlertActionStyle.default, handler:nil)
                alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(alertActin)
            } else if description.portType == AVAudioSessionPortBluetoothLE {
                NSLog("Bluetooth plugged in")

                let alertActin = UIAlertAction(title: description.portName, style: UIAlertActionStyle.default, handler:nil)
                alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(alertActin)
            }
            else {
                NSLog("")
            }
        }

也许您需要指示AVAudioSession首先按照建议允许蓝牙输出


请注意,讨论了AvaudioSessionCategoryPlay和Record的使用,并建议在使用输出设备时使用AvaudioSessionCategoryPlay和Record。

我已将此添加到代码中,但我的蓝牙扬声器仍然没有出现,请执行{try session.setCategory(AvaudioSessionCategoryPlay和Record,带:[.allowBluetooth])try session.setActive(true)}catch{fatalError(“设置音频会话时出错”)}