简体   繁体   English

音频录制的大小限制 AVAudioRecorder swift

[英]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.我尝试使用这种方法,但它会在 2-3 秒内更新一次大小。 But I am trying to update it each 1 second with timer.但我试图用计时器每 1 秒更新一次。 But it returns the same size each time and updates once in 2-3 seconds.但它每次返回相同的大小,并在 2-3 秒内更新一次。

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?有没有办法为 AVAudioRecorder 设置最大文件大小?

This is not possible with AVAudioRecorder .这对于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.或者,您可以使用较低级别的 AVFoundation API 来获取数据并将其写入自己的文件。

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 (eg to 1k or 10k bytes), you'll hide insignificant information and get a better UX.然后,如果您将 UI 上显示的值四舍五入(例如到 1k 或 10k 字节),您将隐藏无关紧要的信息并获得更好的用户体验。

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. LinearPCM 将是最简单的,但使用相当多的存储空间。 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.请务必从设备获取实际采样率,而不是硬编码(48k、44.1k 等),因为这些可能因设备而异。

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 .如果您需要快速/准确的实时信息,那么您需要使用水龙头手动制作自己的录音机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM