AVAudioFile.length is 0 even if file exists

I'm creating an AVAudioFile for writing sound to a sound file. If the file already exists I want to move the framePosition to the end of the file, to continue writing at the end, instead of replacing the existing file.

I check if the file exists, and if it does I set the framePosition property to the same value as the length property. However, the length property is always 0, even if the file exists.

Does creating a new instance of AVAudioFile delete the existing file at the URL you pass in when you create it? What am I missing here?


var boolFileExists = false

if (NSFileManager.defaultManager().fileExistsAtPath(self.recordAudioURL.path!)) {

     boolFileExists = true

}

do {

     try self.audioFile = AVAudioFile(forWriting: self.recordAudioURL, settings: self.engine.mainMixerNode.outputFormatForBus(0).settings)

     if (boolFileExists) {

          self.audioFile.framePosition = self.audioFile.length

     }

} catch let error as NSError {

     NSLog("Error creating AVAudioFile %@", error.localizedDescription)

}


I also tried to load the file for reading as well and write it's buffer to the self.audioFile


let audioFile = try AVAudioFile(forReading: self.audioRecordURL)

let audioFormat = audioFile.processingFormat

let audioFrameCount = UInt32(audioFile.length)

let audioBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)

try self.audioFile.readIntoBuffer(audioBuffer)

self.audioFile.framePosition = self.audioFile.length


But the audioFile.length is 0 here as well. Interestingly, when I try to load another file, not written to while recording in my app, it works. So I'm guessing the problem actually is my sound file. Do I have to "finalize" or "close" the sound file after I have written to it to be able to use it like this? I CAN play it manually, if I click on the actual file in the simulator folders on my computer, by the way.

The AVAudioFile Objective C header documentation makes it clear that all files opened for writing will be overwritten if they already exist


-DS

Thanks for your reply! I'm trying another solution to my problem. I have managed to get the audiobuffer from the existing file, and now I'm trying to read that buffer into a new file, so that I can continue to write at the end of it. But I'm getting a crash when trying to read the buffer into the file:


let audioFile = try AVAudioFile(forReading: [URL to existing .caf file])

let audioFrameCount = AVAudioFrameCount(UInt32(audioFile.length))

let audioBuffer = AVAudioPCMBuffer(PCMFormat: audioFile.processingFormat, frameCapacity: audioFrameCount)

let newAudioFile = try AVAudioFile(forWriting: [URL to a new .caf file], settings: self.engine.mainMixerNode.outputFormatForBus(0).settings)

try newAudioFile.readIntoBuffer(audioBuffer, frameCount: audioFrameCount!) <-- CRASHES ON THIS LINE


I'm getting an -50 error code.


What am I doing wrong? Can't I read a buffer into a file that's created using AVAudioFile(forWriting...) ?


What's the best way to write audio to the end of an existing audio file?

AVAudioFile.length is 0 even if file exists
 
 
Q