【发布时间】:2016-02-24 08:10:07
【问题描述】:
我想从 iPhone 麦克风(NSData 格式)获取原始音频数据以通过套接字流式传输。这不是我可以使用 twilio/etc 的情况,因为它是一个研究项目。套接字实现已完成(我可以发送音频文件),但我无法获取流式麦克风数据。
这是我的尝试:
class ViewController: UIViewController, AVCaptureAudioDataOutputSampleBufferDelegate
{
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupMicrophone()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupMicrophone()
{
let session = AVCaptureSession()
session.sessionPreset = AVCaptureSessionPresetMedium
let mic = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
var mic_input: AVCaptureDeviceInput!
let audio_output = AVCaptureAudioDataOutput()
audio_output.setSampleBufferDelegate(self, queue: dispatch_get_main_queue())
do
{
mic_input = try AVCaptureDeviceInput(device: mic)
}
catch
{
return
}
session.addInput(mic_input)
session.addOutput(audio_output)
session.startRunning()
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)
{
// Do something here
}
}
问题:
永远不会调用委托函数。
提供给委托的数据(如果它被调用)不是 NSData,是否有另一个函数可以提供 NSData?有没有办法将 CMSampleBuffer 转换为 NSData?
感谢任何帮助。
干杯
【问题讨论】: