lolCheck
lolCheck

Reputation: 59

Size limit for audio recording AVAudioRecorder swift

I need to setup max size for file which is recording. I tried to use this method, but it updates the size once in 2-3 seconds. But I am trying to update it each 1 second with timer. But it returns the same size each time and updates once in 2-3 seconds.

func updateSlider() {
   let attr = try FileManager.default.attributesOfItem(atPath: url.path)
   let dict = attr as NSDictionary
   let fileSize = Int(dict.fileSize()) 
}

Is there any way to set max file size for AVAudioRecorder?

Upvotes: 2

Views: 340

Answers (2)

jaybers
jaybers

Reputation: 2211

It would help if you show how you start your recorder so I can see the format of the recorded file.

Assuming you are using a simple lossless recording format, you can infer the file size by using the recording format and the time since recording started. Once you know the recording format (sample rate, number of channels, number of bits per channel), you can associate this to the file size. Without knowing the format of your audio, it is hard to help. LinearPCM would be the easiest but uses quite a bit of storage. Be sure to get the actual sample rates from the device rather than hard coding (48k, 44.1k etc.) as these can vary between devices.

You can automatically stop the recorder after a certain amount of time as computed by the recording format using. Calculate the max time per some file format and stop the recorder if needed.

 recorder.record(forDuration: TimeInterval)

The reason it updates every fews seconds is because the recorder buffers a little bit then writes appends the file. If you need fast/accurate realtime info, then you need to manually make your own recorder using a tap.

Upvotes: 0

meaning-matters
meaning-matters

Reputation: 22966

This is not possible with AVAudioRecorder.

You could calculate the moving average and interpolate the missing values. You could do that at a higher frequency to smooth progress.

Alternatively you could use lower level AVFoundation APIs to get to the data and write that to a file yourself.

Assuming the maximum file size doesn't need to be very accurate, I'd be pragmatic and use the former method.

Then, if you'd round the values shown on the UI (e.g. to 1k or 10k bytes), you'll hide insignificant information and get a better UX.

Upvotes: 2

Related Questions