Can't play a sound file downloaded from an S3 bucket

I am facing a weird problem with an .m4a sound file on an iOS app written in Swift.

Here is what happens:

  • I record a sound file on the device using the app. Let us call it xyz.m4a.
  • I check that I can play this sound file, and it is working.
  • I then upload the sound file to an AWS S3 bucket, from my app.
  • I check on the AWS console that the file is uploaded as expected and I download it using a browser (on my computer), checking again that I can still play the sound as expected. Up to this point nothing is wrong, I can play the sound.
  • I then use my app to download the file to my iOS device (iPhone 6). All seems OK here too.
  • Finally I try to play the downloaded file from within my app and this is where things go wrong. I cannot play it.

Here is what I think is the relevant code, please let me know if you think I should add more.

Code to download the file from the AWS S3 bucket:

    let transferUtility = AWSS3TransferUtility.default()
        fromBucket: "mybucket",
        key: "xyz.m4a",
        expression: expression,
        completionHandler: completionHandler
        ).continueWith {
            (task) -> AnyObject! in if let error = task.error {
                print("Error: \(error.localizedDescription)")

            if let _ = task.result {
                // Do something with downloadTask.
                print("task.result -- OK!")
                let downloadOutput = task.result
                print("downloadOutput:\(String(describing: downloadOutput))")
            return nil;

Here is the completionHandler part:

    var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
    completionHandler = {
        [weak self] (task, URL, data, error) -> Void in
        DispatchQueue.main.async(execute: {
            print("transfer completion OK!")
            let localFileName = "xyz.m4a",
            playFileURL = self?.getDocumentsURL().appendingPathComponent(localFileName)
            FileManager.default.createFile(atPath: (playFileURL?.path)!,
                                           contents: data,
                                           attributes: nil)

            if FileManager.default.fileExists(atPath: (playFileURL?.path)!) {
                print("playFileURL present!") // Confirm that the file is here!

Code to play the downloaded sound file:

    let fileName = "xyz.m4a", 
    audioFile = getDocumentsURL().appendingPathComponent(fileName)

    if !FileManager.default.fileExists(atPath: audioFile.path) {
        print("We have no file !!!!!") // Just in case the file was not found!

    // This code assumes the file exists.
    do {try soundSession.setCategory(AVAudioSessionCategoryPlayback)
        try soundSession.setActive(true)

        if audioPlayer == nil {
            audioPlayer = try AVAudioPlayer(contentsOf: audioFile,
                                            fileTypeHint: AVFileType.m4a.rawValue)
            guard let _ = audioPlayer else {return}
            audioPlayer.delegate = self
    } catch let error {
        print("Error:\n" + error.localizedDescription)

Finally here is the error I get:

The operation couldn’t be completed. (OSStatus error 1685348671.)

I hope someone can see something wrong an tell me where the problem is.