Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream audio from microphone via bluetooth to another iPhone

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.

like image 633
iSkore Avatar asked Jun 09 '15 04:06

iSkore


People also ask

Can you connect a Bluetooth microphone to iPhone?

Using a Bluetooth Mic with Switcher StudioGo to your iOS Settings. Tap Bluetooth in the left menu. If it's not already enabled, tap the slider to turn it on. Under My Devices, tap the name of the bluetooth device you want to connect.

Can iPhone listen to conversations?

“With Live Listen, your iPhone, iPad, or iPod touch can act like a microphone that sends sound to your AirPods, AirPods Pro, AirPods Max, Powerbeats Pro, or Beats Fit Pro. Live Listen can help you hear a conversation in a noisy area or even hear someone speaking across the room.”

Can an external microphone be connected to an iPhone?

To connect an iPhone with an external microphone, grab a mic with a Lightning or TRRS connector. You may need a few adapters, such as the 3.5mm TRS-TRRS adapter and/or a 3.5mm jack to Apple Lightning adapter. By daisy-chaining multiple adapters, you could even connect an XLR mic to your iPhone.


1 Answers

To simultaneously record and redirect output you need to use the category AVAudioSessionCategoryMultiRoute.

Here's the link to the categories list: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/#//apple_ref/doc/constant_group/Audio_Session_Categories

If all else fails you can use a pre-made solution: http://audiob.us

It has an API that lets you integrate audio streaming from one app to another: https://developer.audiob.us/

It supports multiple output endpoints.

Hope this helps.

like image 98
Capstone Avatar answered Oct 21 '22 22:10

Capstone