I was working with recording in iOS and I came across a rather strange problem with re-playing the recordings. What would happen was, I would record the audio, playback the recording, save the audio file and it would all work as expected. Now when I try to relaunch/redeploy the app from Xcode and try to playback the previously recorded file, it wouldn’t work, the problem was that it cannot find the audio recording. As usual I have solved that problem and while I am not sure if it is the right way to do this, but my solution works and I am going to share that solution in this post along with some code samples. Hopefully it saves the next programmer stuck on this problem some time.


I wanted to learn how to record audio in iOS using the microphone so as usual I did a search on Google for tutorials on these and I found some excellent examples on it. Among others the tutorial at  HackingWithSwift was one of the finest tutorials out there. Unfortunately the link to that tutorial is no longer accessible but here’s a handy little code sample that shows recording in iOS. It’s great and it provides some really good insight into how recording works. So thanks to that tutorial in a relatively short time I got to a point where I could,
  1. launch an app on my iPhone
  2. record an audio
  3. save the recording and 
  4. playback the recording
One of my objectives was to persist the location of my recorded audio so the app knows the location of the saved recording so it can be replayed.

p.s. I won’t be going into the details of recording and playing back audio, if you need to know that I would recommend having a look at this.


As I was working working on the app, I saved a bunch of audio recordings and then I realised I had to make some UI changes.  So I stopped running the app on my phone, made some changes to the app and redeployed from Xcode to my iPhone and guess what? I could no longer playback the audio recordings that I made prior to re-deploying the the app from Xcode[link]. It was really strange and I couldn’t understand why this was the case? After some simple debugging I realised that the path to the audio had HexaDecimal UUID which changed every time I would re-deploy the app be it the iPhone or the Simulator via Xcode.


I basically ran the app multiple times and just printed the path to Xcode console and kept a record of the absolute path of the documents directory where my audio recording was being saved to every time. Below are the examples of what the paths looked like each time I ran the app and saved a recording

First run


Second run


Third run



Now my code to do the recording is pretty much identical to the one found on the HackingWithSwift sample code. Some of the differences include,
I had a data structure to save my recording and below is a gist of what it looked like,

class Recording:NSObject,NSCoding { 
var name:String!
var audioPath:String!
I would record some audio, create the Recording data structure and save that as well as the audio recording. 
Here’s my function that creates a name of the audio recording

func getFilenameForAudioTask() -> (name:String, url:URL) {
//Step 1: Get the task name
let now = Date()
let taskName = "(now.timeIntervalSince1970)_recording.m4a"
//Step 2: Get the documents directory
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
//Step 3: create the filename
let audioFilename = documentsDirectory.appendingPathComponent(taskName)
let audioUrl = URL(fileURLWithPath: audioFilename)
return (name: taskName, url:audioUrl)
Now the above code is self explanatory however since the audioPath variable in Recording is a string, I would save the value of path derived from the NSURL which would look something like the above paths.  Have a look the text in bold in the above paths, you see it has a UUID in the path right before the Documents is mentioned in the path. So the problem was that in each run I was trying to access the audio file from a location where it did not exist.


Once I knew what the problem was coming up with the solution that I came up with was very easy. My solution is that instead of trying to access the file from it’s full path, just search the documents directory for a file of type .m4a with the unique name that I assigned to that file.

func getFileURL(fileName:String) -> URL? {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
return directoryContents.first{$0.absoluteString.contains(fileName)}
} catch let error as NSError {
return nil
The url from the method above will be passed to this AVAudioPlayer constructor to playback recorded audio. Once again I won’t be going into the details of recording and playing back audio, if you need to know that I would recommend having a look at this.


Is this the best way to solve the above problem? I am not sure whether or not this is the best way to do this, but this is a solution I have which works. If you know of something better then please let me know or even if you would like to see some sample code of a working solution to the above problem, let me know and I will see what I can do.
Finally, I am working on my app full-time right now so if you find my blog posts useful and want to support me you can 
  • Either buy the complete version of My Day To-Do
  • Or just give the Lite(free) version a try and leave us an app store review
Any feedback on my apps or the way I write my post, would be great.

Leave a Reply

Your email address will not be published. Required fields are marked *