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

iSk*_*ore 15 iphone audio bluetooth ios swift

我正在尝试接收传入的麦克风音频并将其传输到另一部iPhone.基本上是电话,但通过蓝牙.我有音频通过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)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我试图使用StreamReader这里-的StreamReader从@马丁-R

使用:

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

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

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

我将行转换为NSData,然后使用dispatch_after 0.5秒不断运行,我通过蓝牙将其发送到另一台设备.

它似乎不起作用,我不认为这是一种实用的方法.我做了很多搜索,并没有看到很多关于通过蓝牙的流数据.关键词流(可以理解)将我发送到有关服务器流的页面.

我的问题是,如何从麦克风中取出音频并通过蓝牙将其发送到另一部iPhone?我有蓝牙部分都设置好,它很棒.我的问题非常类似于此,除了iPhone和Swift - 我想通过蓝牙拨打电话.

先感谢您.

ˆᵛˆ*_*ˆᵛˆ 8

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

以下是类别列表的链接: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/

它支持多个输出端点.

希望这可以帮助.