1

I have been trying to implement Google's Speech to Text API in iOS and I cam across this piece of code in their AudioController file. This piece of code

    let buffers = UnsafeMutableBufferPointer<AudioBuffer>(start:
                                                            &bufferList.mBuffers, count: Int(bufferList.mNumberBuffers))
    
    buffers[0].mNumberChannels = 1
    buffers[0].mDataByteSize = inNumberFrames * 2
    buffers[0].mData = nil

gives me this error

Initialization of 'UnsafeMutableBufferPointer<AudioBuffer>' results in a dangling buffer pointer

I've tried the solutions over on this question Warning: Initialization of 'UnsafeBufferPointer<T>' results in a dangling buffer pointer, but the solutions don't work as intended.

Any help?

Tanshik
  • 17
  • 4

1 Answers1

1

I am using the same AudioController. I have found the solution here

Code can be updated like this:

let buffers = withUnsafeMutablePointer(to: &bufferList.mBuffers) {
    UnsafeMutableBufferPointer(start: $0, count: Int(bufferList.mNumberBuffers))
  }
  ...
  ...    
  // get the recorded samples
  status = withUnsafeMutablePointer(to: &bufferList) {
      AudioUnitRender(AudioController.sharedInstance.remoteIOUnit!,
                               ioActionFlags,
                               inTimeStamp,
                               inBusNumber,
                               inNumberFrames,
                               $0)
  }

Works for me fine

Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39