Skip to main content

Recording audio in iOS: Problem finding recorded audio

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.

Introduction

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.

Problem


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.

Debugging

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

Users/macbookAir/Library/Developer/CoreSimulator/Devices/FAC34A89-2454-4EBD-8B7A-5029BC5C3DED/data/Containers/Data/Application/843AD233-BA10-4E84-A4F1-A8C0D575A6C6/Documents/1480306328.34143_recording.m4a

Second run


/Users/macbookAir/Library/Developer/CoreSimulator/Devices/FAC34A89-2454-4EBD-8B7A-5029BC5C3DED/data/Containers/Data/Application/6782F31D-287D-4644-B50E-30F51F130E2C/Documents/1480306440.46182_recording.m4a

Third run


/Users/macbookAir/Library/Developer/CoreSimulator/Devices/FAC34A89-2454-4EBD-8B7A-5029BC5C3DED/data/Containers/Data/Application/2F9965ED-852C-4484-A8F8-B8942E140AAB/Documents/1480306473.06089_recording.m4a

Analysis

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.

Solution

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 {
        print(error.localizedDescription)
    }
    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.

Conclusion


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.

Comments

Popular posts from this blog

Upload to AWS S3 from Java API

In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing. Problem Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that. Read more below,  Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do (mydaytodo.com)

Html5 based widget for an iOS app: Today extension powered by the Ionic framework

At some point the thought of adding a Widget to my iOS app came to mind which was followed by starting work on adding a widget for my app, My Day Todos . Obviously the first step was to learn how to add a Widget to an iOS app and in that learning process I discovered many things about widgets in iOS first of which was a widget in iOS is a an app extension i.e a  Today Extension . While I am still haven't finished working on the widget for my iOS app, I thought I would take some time out and share what I have learned. In this post, I will share a few important tips and provide an example of how to add a Widget to an iOS app and have the widget UI powered by Html5 via  Ionic framework . I added some code to my Github repo, Html5StarterAppWithSwift in order to show how this can be achieved. There are already too many tutorials out on the Web on how to add a Today extension to an iOS app so I won't be including that here. Instead I will focus on sharing some of the useful tip...

Build a Full-Stack Image Upload App with Node.js, Express, React, and Vite (Beginner Tutorial)

 If you’re new to full-stack web development and want a hands-on project to practice React frontend integration with a Node.js + Express backend , this tutorial is for you. In this guide, we’ll walk through a simple but powerful app that lets users upload images, store them on the server, and display them back in the browser. This project is based on my GitHub repo: node-express-react-simple-fileupload . It’s designed to be beginner-friendly, SEO-optimized, and a great starting point for anyone learning JavaScript full-stack development . 🛠️ Technologies Used Here’s the tech stack powering this project: Node.js – JavaScript runtime for the backend. Express.js – Lightweight web framework for building REST APIs. Multer – Middleware for handling file uploads. CORS – Enables cross-origin requests between frontend and backend. React.js – Frontend library for building user interfaces. Vite – Fast development server and build tool for React. Fetch API – For making HTTP requests ...