在AVAudioSession中使用蓝牙播放

时间:2015-07-30 11:18:09

标签: ios swift avaudiosession avaudioengine

我尝试使用AVAudioSession使用蓝牙设备播放语音。这是我在appdelegate.swift' s didFinishLaunchingWithOptions和viewcontroller.swift(一次一次)中尝试放入的代码

  import AVFoundation
var audioSession:AVAudioSession = AVAudioSession.sharedInstance() 

 audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.AllowBluetooth, error: nil)

因此代码执行完美但不使用连接的蓝牙设备。实际上,我在iPhone上打开控制中心,当应用程序未打开时,音频源显示为蓝牙设备,但是一旦应用程序打开,蓝牙设备选项就会消失...

其余代码(如果有任何用途)是 -

var engine = AVAudioEngine() 
var input = engine.inputNode 
var output = engine.outputNode 
var format = input.inputFormatForBus(0) 
var error:NSError? 
var audioSession:AVAudioSession = AVAudioSession.sharedInstance()    engine.startAndReturnError(&error)

它基本上是一个现场音频播放器,它从麦克风中取出声音并播放到AUX,扬声器,(蓝牙)

1 个答案:

答案 0 :(得分:1)

你是对的,设置选项AllowBluetooth是在你的应用程序中打开蓝牙的唯一方法。但是蓝牙可能存在一些问题:

  1. 您的蓝牙设备不支持A2DP协议(高级音频分发配置文件),您可以通过标准音乐应用播放音乐进行检查。
  2. 如果您设置preferredBufferDurationprefferedSampleRate,也可能会影响蓝牙。
  3. 也许问题在于此。