获取iPhone麦克风数据以通过Socket流式传输 [英] Get iPhone mic data for streaming over Socket
问题描述
我想从iPhone麦克风(以NSData格式)获取原始音频数据,以通过套接字流式传输.这不是我可以使用twilio/etc的情况,因为它是一个研究项目.套接字实现已完成(我可以发送音频文件),但是我无法获取流式麦克风数据.
I would like to get raw audio data from the iPhone mic (in NSData format) to stream over a socket. This is not a situation where I can use twilio/etc, as it is a research project. The socket implementation is done (I can send audio files), but I'm having trouble getting the streaming mic data.
这是我的尝试:
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
}
}
问题:
-
从不调用委托函数.
The delegate function is never called.
提供给委托的数据(如果正在被调用)不是NSData,是否还有另一个函数可以提供NSData?有没有办法将CMSampleBuffer转换为NSData?
The data given to the delegate (if it were being called) is not NSData, is there another function that would provide NSData? Is there a way to convert CMSampleBuffer to NSData?
感谢您的帮助.
欢呼
推荐答案
您的 AVCaptureSession
超出范围并被释放.这就是为什么不呼叫您的代表的原因.您可以通过将 session
移至类范围来解决此问题:
Your AVCaptureSession
is going out of scope and being deallocated. That's why your delegate isn't being called. You can fix this by moving session
to class scope:
class ViewController: UIViewController, AVCaptureAudioDataOutputSampleBufferDelegate {
let session = AVCaptureSession()
override func viewDidLoad() {
一旦有了音频 CMSampleBuffer
,就可以将音频数据复制到 NSData
对象中,如下所示:
Once you have an audio CMSampleBuffer
, you can copy the audio data into an NSData
object like this:
let block = CMSampleBufferGetDataBuffer(sampleBuffer)
var length = 0
var data: UnsafeMutablePointer<Int8> = nil
let status = CMBlockBufferGetDataPointer(block!, 0, nil, &length, &data) // TODO: check for errors
let result = NSData(bytes: data, length: length)
p.s.如果您小心翼翼并希望避免复制,则可以使用 NSData(bytesNoCopy:data,length:length,freeWhenDone:false)
p.s. if you're careful and want to avoid copying, you can use NSData(bytesNoCopy: data, length: length, freeWhenDone: false)
这篇关于获取iPhone麦克风数据以通过Socket流式传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!