I'm able to record and play audio and though I'm trying to save the file after trimming, note that this function will re-save the file regardless of being trimmed. Here is the error being thrown.
failed Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x600001635aa0 {Error Domain=NSOSStatusErrorDomain Code=-16979 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16979)
Here is my setup
var recordingURL: URL!
let fileName = "\(NSUUID().uuidString).m4a"
func startRecording() {
recordingURL = FileManager.getDocumentsDirectory().appendingPathComponent(fileName)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: recordingURL, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
} catch {
finishRecording(success: false)
}
}
func saveAudiofile() {
let asset = AVAsset(url: recordingURL)
exportAsset(asset)
}
func exportAsset(_ asset: AVAsset) {
let documentsDirectory = FileManager.getDocumentsDirectory()
let fileURL = documentsDirectory.appendingPathComponent(fileName)
print("Here is the file Path \(fileURL.path)")
let filePath = fileURL.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
do {
try fileManager.removeItem(atPath: filePath)
}
catch {
print("Couldn't remove existing destination file: \(error)")
}
}
print("creating export session")
if let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) {
exporter.outputFileType = AVFileType.m4a
exporter.outputURL = fileURL
if wastrimmed {
let start = CMTime(seconds: startTime, preferredTimescale: 1)
let stop = CMTime(seconds: endTime, preferredTimescale: 1)
exporter.timeRange = CMTimeRangeFromTimeToTime(start: start, end: stop)
} else {
let startTime = CMTime(seconds: 0, preferredTimescale: 1)
let endTime = CMTime(seconds: recordingSnapshot, preferredTimescale: 1)
exporter.timeRange = CMTimeRangeFromTimeToTime(start: startTime, end: endTime)
print("The duration is: \(exporter.timeRange.duration)")
}
let trimmedDuration = CMTimeGetSeconds(exporter.timeRange.duration)
if trimmedDuration < 10.0 {
print("Audio is not long enough: \(trimmedDuration)")
return
}
exporter.exportAsynchronously(completionHandler: {
switch exporter.status {
case AVAssetExportSessionStatus.failed:
print("failed \(exporter.error!)")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(exporter.error!)")
case AVAssetExportSessionStatus.unknown:
print("unknown\(exporter.error!)")
case AVAssetExportSessionStatus.waiting:
print("waiting\(exporter.error!)")
case AVAssetExportSessionStatus.exporting:
print("exporting\(exporter.error!)")
default:
print("complete")
User.draftEpisodes?.append(self.fileName)
}
})
} else {
print("cannot create AVAssetExportSession for asset \(asset)")
}
}
The output:
Success recording Here is the file Path /Users/waylansands/Library/Developer/CoreSimulator/Devices/E919B236-0B88-4CA4-A7EF-03E668AE716E/data/Containers/Data/Application/E6410C85-49EF-4F65-83D2-C470C166BBAE/Documents/16E8D6E6-DB81-48DA-8766-7A60981031EC.m4a
creating export session 2020-04-20 20:53:00.218660+1000 Dune[4381:496890] CMTimeMakeWithSeconds(11.173 seconds, timescale 1): warning: error of -0.173 introduced due to very low timescale
The duration is: CMTime(value: 11, timescale: 1, flags: __C.CMTimeFlags(rawValue: 3), epoch: 0)
failed Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x600001635aa0 {Error Domain=NSOSStatusErrorDomain Code=-16979 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16979), NSURL=file:///Users/waylansands/Library/Developer/CoreSimulator/Devices/E919B236-0B88-4CA4-A7EF-03E668AE716E/data/Containers/Data/Application/E6410C85-49EF-4F65-83D2-C470C166BBAE/Documents/16E8D6E6-DB81-48DA-8766-7A60981031EC.m4a, NSLocalizedDescription=The operation could not be completed}