2
votes

I'm trying to play the voice using a bluetooth device using AVAudioSession. Here is my code which I've tried putting in both appdelegate.swift's didFinishLaunchingWithOptions and viewcontroller.swift (once at a time)

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

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

So the code executes perfectly but does not use the connected bluetooth device. Actually, I opened control center on iPhone which says audio source as bluetooth device when app is not opened, but bluetooth device option disappears as soon as app is opened...

The rest of code(if thats of any use) is-

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)

It basically is a live audio player, which takes voice from mic and plays it to AUX, speaker, (bluetooth)

1

1 Answers

1
votes

You are right, setting option AllowBluetooth is one and only way to turn on bluetooth in your application. But there could be a few problems with bluetooth:

  1. Your bluetooth device does not support protocol A2DP (Advanced Audio Distribution Profile), you can check it by playing music through standard Music app.
  2. If you setup preferredBufferDuration or prefferedSampleRate it also may affect bluetooth.

Perhaps the problem lies in this.