iOS中的蓝牙语音 [英] Voice over bluetooth in iOS

查看:209
本文介绍了iOS中的蓝牙语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行为期四天的研究,但没有找到在远处的两个iOS设备之间通过蓝牙进行呼叫的任何解决方案.

I am doing research over four days, But I am not found any solution for calling over Bluetooth between two iOS devices within a distance.

我发现使用对等连接框架可以在两个iOS设备之间进行音频流传输,但这对我没有帮助.我想通过蓝牙在两个设备之间进行实时语音聊天.

I found that audio streaming is possible between two iOS devices using multipeer connectivity framework but this is not helpful for me. I want real time voice chat between two devices over Bluetooth.

是否有用于蓝牙语音的CO-DAC?

Is there any CO-DAC for voice over Bluetooth?

我的代码是:

     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对您没有帮助?这是通过蓝牙甚至wifi传输音频流的好方法.

Why is MultipeerConnectivity not helpful for you? It is a great way to stream audio over bluetooth or even wifi.

致电时:

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

您需要使用类型为AVAudioPCMBuffer的缓冲区.然后,您需要将其转换为NSData并写入您将与对等方打开的outputStream:

You need to use the buffer, which has type AVAudioPCMBuffer. You then need to convert that to NSData and write to the outputStream that you would've opened with the peer:

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

然后在另一台设备上,您需要从流中读取并将NSData转换回AVAudioPCMBuffer,然后可以使用AVAudioPlayer播放缓冲区.

Then on the other device you need to read from the stream and convert from NSData back to an AVAudioPCMBuffer, and then you can use an AVAudioPlayer to playback the buffer.

我之前做到这一点的延迟非常短.

I have done this before with a very minimal delay.

这篇关于iOS中的蓝牙语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆