我希望能从iPhone麦克风获取原始音频数据(以NSData格式),并通过套接字进行流传输。这不是可以使用twilio等服务的情况,因为这是一个研究项目。套接字实现已完成(我可以发送音频文件),但我在获取流式麦克风数据方面遇到了问题。
以下是我的尝试:
以下是我的尝试:
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?
非常感谢您的帮助。
谢谢!
bytesNoCopy:
在完成后调用free
。你需要使用bytesNoCopy:length:freeWhenDone:
。正在更新答案。 - Rhythmic Fistman