iOS上的蓝牙语音

我正在做四天的研究,但我没有find任何解决scheme,在两个iOS设备之间远程调用蓝牙。

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

有没有蓝牙语音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 } 

提前致谢 :)

为什么MultipeerConnectivity对你没有帮助? 这是一个伟大的方式来通过蓝牙或甚至WiFistreamaudio。

当你调用这个:

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

您需要使用types为AVAudioPCMBuffer的缓冲区。 然后,您需要将其转换为NSData,然后写入您将用对等打开的outputStream:

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

然后,在其他设备上,您需要从stream中读取数据,然后从NSData转换回AVAudioPCMBuffer,然后使用AVAudioPlayer播放缓冲区。

我之前做了这个,时间很短。