Search code examples
swiftaudiokit

AKAudioFile exportAsynchronously path errors


I'm trying to use AKAudioFile.exportAsynchronously to convert wav to m4a (based on the sample code here: https://audiokit.io/playgrounds/Playback/Exporting%20Files/). I've chosen .documents as my BaseDirectory, but I just keep getting directory <my_dir> isn't valid errors — e.g.:

AKAudioFile+ProcessingAsynchronously.swift:exportAsynchronously(name:baseDir:exportFormat:fromSample:toSample:callback:):379:ERROR AKAudioFile export: directory "/var/mobile/Containers/Data/Application/20C913AD-B2F4-4F26-AAD2-0DFA0C65A886/Documents/All Of Me.mp4" isn't valid

That URL looks completely reasonable, to me, so what's up?


Solution

  • Okay, following @jake's tip, the solution was to handle the spaces explicitly before passing into AKAudioFile's exportAsynchronously(name:baseDir:exportFormat:callback:). I just did:

    var name = String(cafURL.lastPathComponent.split(separator: ".")[0])
    name = name.replacingOccurrences(of: " ", with: "%20")
    let exportFile = try AKAudioFile(readFileName: "\(name).wav", baseDir: .documents)
    exportFile.exportAsynchronously(name: name, baseDir: .documents, exportFormat: .m4a, callback: self.callback)