I am trying to take incoming microphone audio and stream it to another iPhone. Basically a phone call but via bluetooth. I have the audio coming in via AVAudioRecorder
:
func startRecording() {
audioRecorder = nil
let audioSession:AVAudioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryRecord, error: nil)
var recordSettings:NSMutableDictionary = NSMutableDictionary(capacity: 10)
recordSettings.setObject(NSNumber(integerLiteral: kAudioFormatLinearPCM), forKey: AVFormatIDKey)
recordSettings.setObject(NSNumber(float: 44100.0), forKey: AVSampleRateKey)
recordSettings.setObject(NSNumber(int: 2), forKey: AVNumberOfChannelsKey)
recordSettings.setObject(NSNumber(int: 16), forKey: AVLinearPCMBitDepthKey)
recordSettings.setObject(NSNumber(bool: false), forKey: AVLinearPCMIsBigEndianKey)
recordSettings.setObject(NSNumber(bool: false), forKey: AVLinearPCMIsFloatKey)
soundPath = documentsDirectory.stringByAppendingPathComponent("record.caf")
refURL = NSURL(fileURLWithPath: soundPath as String)
var error:NSError?
audioRecorder = AVAudioRecorder(URL: refURL, settings: recordSettings as [NSObject : AnyObject], error: &error)
if audioRecorder.prepareToRecord() == true {
audioRecorder.meteringEnabled = true
audioRecorder.record()
} else {
println(error?.localizedDescription)
}
}
Then I tried using StreamReader
from HERE - StreamReader from @martin-r
Using:
if let aStreamReader = StreamReader(path: documentsDirectory.stringByAppendingPathComponent("record.caf")) {
while let line = aStreamReader.nextLine() {
let dataz = line.dataUsingEncoding(NSUTF8StringEncoding)
println (line)
Then send the data to another device using:
self.appDelegate.mpcDelegate.session.sendData(data: NSData!, toPeers: [AnyObject]!, withMode: MCSessionSendDataMode, error: NSErrorPointer )
I convert line to NSData, then using a dispatch_after 0.5 seconds running constantly, I send it to another device via bluetooth.
It does not seem to work and I don't think this is a practical way of doing it. I have done numerous searches and haven't seen much on streaming data via bluetooth. The key word streaming (understandably) sends me to pages about server streaming.
My question is, how can I take audio from a microphone and send it to another iPhone via bluetooth? I have the bluetooth part all set up and it works great. My question is very similar to THIS except with iPhones and Swift - I want to have a phone call via bluetooth.
Thank you in advance.