FormatConverter from .wav to .m4a

Viewed 12

I´m working on an app in which I need to upload an audio file to a remote server.

So far the recording and file creation part is working fine, the problem is that the resulting audio is a very heavy .wav file and it will take a very long time to upload it. Thus I´m trying to convert it to a .m4a file before uploading.

Right now I´m using this to convert the file:

     func startConvert() {
       
         let fileName = NSUUID().uuidString
         self.convertedPath = getDocumentsDirectory().appendingPathComponent((recordingName?.appending(".m4a") ?? fileName.appending(".m4a")))
         
         var options = FormatConverter.Options()
         options.format = AudioFileFormat.m4a
    
         guard let outputURL = self.convertedPath else {
             print("error no output path")
             return
         }
         let inputURL = self.recordingPath
         
         convert(inputURL: inputURL!, outputURL: outputURL, options: options)
     }
    private func convert(inputURL: URL, outputURL: URL, options: FormatConverter.Options) {
        let converter = FormatConverter(inputURL: inputURL, outputURL: outputURL, options: options)
        converter.start(completionHandler: { error in
            if let error = error {
                print("Error: \(error)")
            } else {
                print("Success!")
            }
        })
    }

I do get a "Success" and a .m4a file back, but the audio file seems corrupt, it can not be played.

Does somebody knows what am I doing wrong? Any help in this regard is highly appreciated.

0 Answers
Related