通过蓝牙将音频从麦克风流式传输到另一个 iPhone

Stream audio from microphone via bluetooth to another iPhone

我正在尝试获取传入的 microphone 音频并将其流式传输到另一个 iPhone。基本上是 phone 呼叫,但通过蓝牙。我的音频来自 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)
    }
}

然后我尝试使用来自@martin-r

HERE - StreamReaderStreamReader

使用:

if let aStreamReader = StreamReader(path: documentsDirectory.stringByAppendingPathComponent("record.caf")) {
                while let line = aStreamReader.nextLine() {
                    let dataz = line.dataUsingEncoding(NSUTF8StringEncoding)
                    println (line)

然后使用以下方式将数据发送到另一台设备:

self.appDelegate.mpcDelegate.session.sendData(data: NSData!, toPeers: [AnyObject]!, withMode: MCSessionSendDataMode, error: NSErrorPointer )

我将 line 转换为 NSData,然后使用 dispatch_after 0.5 秒 运行 不断地,我通过蓝牙将它发送到另一个设备。

它似乎不起作用,我认为这不是一种实用的方法。我进行了大量搜索,但没有看到太多关于通过蓝牙传输数据的信息。关键字流(可以理解)将我带到有关服务器流的页面。

我的问题是,如何从微型 phone 获取音频并通过蓝牙将其发送到另一个 iPhone?我已经设置好蓝牙部分,效果很好。我的问题与 THIS 非常相似,除了 iPhones 和 Swift - 我想通过蓝牙进行 phone 通话。

提前致谢。

要同时记录和重定向输出,您需要使用类别 AVAudioSessionCategoryMultiRoute

这是类别列表的 link: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/#//apple_ref/doc/constant_group/Audio_Session_Categories

如果一切都失败了,您可以使用预制解决方案: http://audiob.us

它有一个 API 可以让您将音频流从一个应用程序集成到另一个应用程序: https://developer.audiob.us/

它支持多个输出端点。

希望这对您有所帮助。