我正在尝试获取麦克风输入的音频并将其流式传输到另一台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)
}
}
然后我尝试使用这里 - 来自@martin-r 的 StreamReader。
使用:
if let aStreamReader = StreamReader(path: documentsDirectory.stringByAppendingPathComponent("record.caf")) {
while let line = aStreamReader.nextLine() {
let dataz = line.dataUsingEncoding(NSUTF8StringEncoding)
println (line)
然后使用以下方法将数据发送到另一个设备:
self.appDelegate.mpcDelegate.session.sendData(data: NSData!, toPeers: [AnyObject]!, withMode: MCSessionSendDataMode, error: NSErrorPointer )
我将线路转换为NSData,然后使用dispatch_after 0.5秒不断运行,通过蓝牙发送到另一设备。
这似乎没有起作用,而且我认为这不是一个实际的方法。我已经进行了多次搜索,并没有看到有关通过蓝牙流式传输数据的信息。理解上,关键词“streaming”会将我带到有关服务器流式传输的页面。
我的问题是,我该如何从麦克风中获取音频并通过蓝牙发送到另一个iPhone?我已经设置好了蓝牙部分,它的效果很好。我的问题与THIS非常相似,只是涉及到使用Swift在iPhone之间进行电话通话。
提前感谢您的回答。