如何从 Swift 中 API 的录制音频中获取 m4a 文件?

How to get m4a file from recorded audio for API in Swift?

目标:我想录音10秒,获取录音文件并在post请求中发送。

我做了什么:我可以使用以下代码录制 10 秒的音频:

var recordingSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!

func setupAV() {
    recordingSession = AVAudioSession.sharedInstance()

    do {
        try recordingSession.setCategory(.playAndRecord, mode: .default)
        try recordingSession.setActive(true)
        recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.recordTapped)))
                } else {
                    // failed to record!
                }
            }
        }
    } catch {
        // failed to record!
    }
}

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}

@objc func recordTapped() {
    let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a")

    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

    do {
        audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
        audioRecorder.delegate = self
        audioRecorder.record(forDuration: 10)
        print("RECORDING")
    } catch {
        print("audio failed to record")
    }

}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    print("DONE")
}

我需要做什么:我需要将此录音作为 m4a 文件并在 post 请求中发送。我怎样才能做到这一点?如果您需要任何其他信息,请告诉我!谢谢!

您需要使用 AVAssetExportSession 导出录音。这是一个很好的方法,可以为您完成:

func export(fileType: AVFileType = .m4a, completion: @escaping (() -> Void)) {
    let asset = AVAsset(url: recordingFileURL))
    guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else { return }
    exportSession.outputFileType = fileType
    exportSession.metadata = asset.metadata
    exportSession.shouldOptimizeForNetworkUse = true
    exportSession.outputURL = exportOutputURL
    exportSession.exportAsynchronously {
        print("Export finished.")
        completion()
    }
}

其中 exportOutputURL 可以是您在文档目录中创建的任何 URL。例如,我们可以将其定义为计算的 属性:

var exportOutputURL: URL? {
    let pathURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    return pathURL?.appendingPathComponent("my_exported_recording")
}

您现在将在 exportOutputURL 中拥有导出的 m4a 文件。

根据您的 API 期望的参数,您可以像这样转换成数据:

do {
    let data = try Data(contentsOf: exportOutputURL)
} catch { ... }

然后到 base64 字符串:

let base64String = data.base64EncodedString()