2017-07-24 69 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