iOS 录音记录和分块

您的问题可以分为两部分:记录和分块

要通过麦克风录音并写入文件,可以快速开始使用AVAudioEngine和AVAudioFile.请参见下面的示例,该示例以设备的默认输入采样率(您可能需要对其进行速率转换)记录大块.

当您谈论块之间的差异"时,您指的是将音频数据分成几部分的能力,这样当您将它们连接在一起时,您不会听到不连续的声音.例如LPCM音频数据可以在样本级别划分为多个块,但是LPCM比特率很高,因此您更有可能使用诸如adpcm(在iOS上称为ima4?),mp3或aac之类的打包格式.这些格式只能在数据包边界上划分,例如比如说64、576或1024个样本.如果您编写的块没有标题(通常用于mp3和aac,不确定ima4),则连接是微不足道的:只需完全像cat命令行工具那样将块首尾相连即可.可悲的是,在iOS上没有mp3编码器,因此aac可能会成为您的格式,但这取决于您的播放要求. iOS设备和Mac肯定可以播放它.

import AVFoundation

class ViewController: UIViewController {

    let engine = AVAudioEngine()

    struct K {
        static let secondsPerChunk: Float64 = 10
    }

    var chunkFile: AVAudioFile! = nil
    var outputFramesPerSecond: Float64 = 0  // aka input sample rate
    var chunkFrames: AVAudioFrameCount = 0
    var chunkFileNumber: Int = 0

    func writeBuffer(_ buffer: AVAudioPCMBuffer) {
        let samplesPerSecond = buffer.format.sampleRate

        if chunkFile == nil {
            createNewChunkFile(numChannels: buffer.format.channelCount, samplesPerSecond: samplesPerSecond)
        }

        try! chunkFile.write(from: buffer)
        chunkFrames += buffer.frameLength

        if chunkFrames > AVAudioFrameCount(K.secondsPerChunk * samplesPerSecond) {
            chunkFile = nil // close file
        }
    }

    func createNewChunkFile(numChannels: AVAudioChannelCount, samplesPerSecond: Float64) {
        let fileUrl = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("chunk-\(chunkFileNumber).aac")!
        print("writing chunk to \(fileUrl)")

        let settings: [String: Any] = [
            AVFormatIDKey: kAudioFormatMPEG4AAC,
            AVEncoderBitRateKey: 64000,
            AVNumberOfChannelsKey: numChannels,
            AVSampleRateKey: samplesPerSecond
        ]

        chunkFile = try! AVAudioFile(forWriting: fileUrl, settings: settings)

        chunkFileNumber += 1
        chunkFrames = 0
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let input = engine.inputNode!

        let bus = 0
        let inputFormat = input.inputFormat(forBus: bus)

        input.installTap(onBus: bus, bufferSize: 512, format: inputFormat) { (buffer, time) -> Void in
            DispatchQueue.main.async {
                self.writeBuffer(buffer)
            }
        }

        try! engine.start()
    }
}

这篇关于iOS录制音频并实时将其拆分为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

你可能感兴趣的:(iOS 录音记录和分块)