【问题标题】:Get iPhone mic data for streaming over Socket获取 iPhone 麦克风数据以通过 Socket 进行流式传输
【发布时间】: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?

感谢任何帮助。

干杯

【问题讨论】:

    标签: ios swift sockets voip


    【解决方案1】:

    您的AVCaptureSession 超出范围并被释放。这就是为什么没有调用您的代表的原因。您可以通过将 session 移动到类范围来解决此问题:

    class ViewController: UIViewController, AVCaptureAudioDataOutputSampleBufferDelegate {
    
       let session = AVCaptureSession()
    
       override func viewDidLoad() {
    

    一旦你有一个音频CMSampleBuffer,你可以像这样将音频数据复制到一个NSData对象中:

    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)
    

    附言如果你小心并且不想复制,你可以使用NSData(bytesNoCopy: data, length: length, freeWhenDone: false)

    【讨论】:

    • 这看起来可能是问题所在。我会测试并回复你!
    • @rythmic-fistman 第一部分完美运行!但是,当我使用第二部分时,我收到错误: malloc: *** error for object 0x1035662c0: pointer being free was not assigned *** set a breakpoint in malloc_error_break to debug 知道为什么会这样吗?
    • @rythmic-fistman 发生这种情况是因为我正在尝试 bytesNoCopy: 版本 - 切换回 bytes: 已修复!
    • 我的错 - bytesNoCopy: 完成后调用 free。你需要bytesNoCopy:length:freeWhenDone:。正在更新答案。
    • @rythmic-fistman 漂亮!现在我们只需要让它在另一边播放流音频! :S
    猜你喜欢
    • 2018-07-29
    • 1970-01-01
    • 2015-08-23
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多