2017-07-24 50 views
0

我正在做四天的研究,但是我没有发现任何解决方案可以在远处的两台iOS设备之间通过蓝牙进行呼叫。iOS上的蓝牙语音服务

我发现使用多点连接框架的两个iOS设备之间可以进行音频流传输,但这对我没有帮助。我希望通过蓝牙实现两台设备之间的实时语音聊天。

是否有蓝牙语音CO-DAC?

我的代码是:

 var engine = AVAudioEngine() 
     var file: AVAudioFile? 
     var player = AVAudioPlayerNode() 
     var input:AVAudioInputNode? 
     var mixer:AVAudioMixerNode? 

override func viewDidLoad() { 
     super.viewDidLoad() 
     mixer = engine.mainMixerNode 
     input = engine.inputNode 
     engine.connect(input!, to: mixer!, format: input!.inputFormat(forBus: 0)) 
} 

@IBAction func btnStremeDidClicked(_ sender: UIButton) { 
mixer?.installTap(onBus: 0, bufferSize: 2048, format: mixer?.outputFormat(forBus: 0), block: { (buffer: AVAudioPCMBuffer, AVAudioTime) in 
      let byteWritten = self.audioBufferToData(audioBuffer: buffer).withUnsafeBytes { 
       self.appDelegate.mcManager.outputStream?.write($0, maxLength: self.audioBufferToData(audioBuffer: buffer).count) 
      } 
      print(byteWritten ?? 0) 
      print("Write") 
     }) 
     do { 
      try engine.start() 
     }catch { 
      print(error.localizedDescription) 
     } 
} 

func audioBufferToData(audioBuffer: AVAudioPCMBuffer) -> Data { 
     let channelCount = 1 
     let bufferLength = (audioBuffer.frameCapacity * audioBuffer.format.streamDescription.pointee.mBytesPerFrame) 

     let channels = UnsafeBufferPointer(start: audioBuffer.floatChannelData, count: channelCount) 
     let data = Data(bytes: channels[0], count: Int(bufferLength)) 

     return data 
    } 

感谢提前:)

+1

蓝牙低功耗不太可能维持实时语音所需的传输速率。 – Paulw11

回答

2

为什么MultipeerConnectivity对你没有什么帮助?这是通过蓝牙或甚至WiFi传输音频的好方法。

当调用此:

audioEngine.installTap(onBus: 0, bufferSize: 17640, format: localInputFormat) { 
    (buffer, when) -> Void in 

您需要使用缓冲,其类型为AVAudioPCMBuffer。然后,您需要将其转换成NSData的和写,你会已经与同行拉开的OutputStream:

data = someConverstionMethod(buffer) 
_ = stream!.write(data.bytes.assumingMemoryBound(to: UInt8.self), maxLength: data.length) 

然后在其他设备上,你需要从流读取和NSData的转换回一个AVAudioPCMBuffer ,然后您可以使用AVAudioPlayer播放缓冲区。

我之前做过这个工作的时间非常短。

+0

是的,它流式音频通过蓝牙,流媒体我使用https://github.com/tonyd256/TDAudioStreamer,我成功地转移语音对方。首先我使用AVAudioRecoder录制音频,然后将音频流式传输到连接的对等设备。但问题是,TDAudioStramer只能传输.mp3格式,我的录音保存为.m4a格式。因此,我将m4a格式转换为mp3格式并发送到另一端,当音频长度很长时,转换需要太多时间,并且延迟时间超过10秒,因此看起来不像现场。 –

+0

将文件m4a转换为mp3我使用的是https://github.com/lixing123/ExtAudioFileConverter这个类。非常感谢你的回复。请帮帮我。我研究超过5天,但我找不到任何实时音频流的解决方案。 –

+0

你为什么要流式传输mp3或m4a音频。你需要流式传输实际的缓冲数据......我将在上面编辑我的答案。 – Logan