2017-08-02 99 views
1

我正在使用以下代碼來檢測可用輸出,並且它沒有拿起連接的藍牙設備。我知道我的藍牙設備已正確連接,因爲我正在播放音樂。AVAudioSession未檢測到連接的藍牙設備

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("") 
      } 
     } 

回答

0

也許你需要指示AVAudioSession先允許藍牙輸出,建議here

請注意,this post會話使用AVAudioSessionCategoryPlayAndRecord,並建議在使用輸出設備時使用AVAudioSessionCategoryPlayback。

+0

我將此添加到我的代碼,但我的藍牙音箱仍然沒有出現DO { \t \t \t嘗試session.setCategory(AVAudioSessionCategoryPlayAndRecord,具有:.allowBluetooth]) \t \t \t嘗試session.setActive(真) \t \t} {趕上 \t \t \t fatalError( 「錯誤設置音頻會話」) \t \t} – user1079052