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)

Addressing app review rejections for auto-renewing subscription in-app purchase (iOS)

The ability to know what the weather is like while planning your day is a feature of  My Day To-Do  Pro and as of the last update it’s also a part of the  Lite version . Unlike the Pro version it’s an auto-renewing subscription based  in-app purchase (IAP)  in the Lite version. What means is that when a user purchases it, the user only pays for the subscription duration after which the user will be automatically charged for the next period. Adding an  auto-renewing  subscription based IAP proved to be somewhat challenging in terms of the app store review i.e. the app update was rejected by the App Review team thrice because of missing information about the IAP. Therefore in this post I will share my experiences and knowledge of adding auto-renewing IAP in hopes to save someone else the time that I had to spend on this problem. In-App purchase This year I started adding IAPs to My Day To-Do Lite which lead to learning about different types of IAP...

Getting started with iOS programming using Swift (Part 1)

I have not been too fond of Objective-C, which was the primary reason for me to stay away from making iOS apps till now. So what changed? Well Apple has done something very interesting recently and that is the introduction of a new programming language i.e. Swift. Swift is awesome, it almost feels like Python, C++ and Objective-C had a baby with some of their good parts in them. So I have been getting to know Swift and it is an awesome language to program in. What I am going to share with this and a series of blog posts are solutions to some problems that i have encounter while i am trying to finish my first iOS app. The one hurdle that I have encountered while getting started on developing an iOS app is that a majority of the solutions for iOS specific problems provide solutions to them using Objective-C. Which is fair, because Swift has not been around for that long. Anyway let us get started with a few basics, A few basics I would highly recommend having a read of this book...