1

I can successfully record audio using AVAudioRecorder. I can successfully play this recording using AVAudioPlayer. This is saved to the documents folder:

FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

However, when i try to re-open this file (to either record over it or play it back) i am unable to do so. I get recording state or player state to return false.

An example of the file name/URL:

file:///var/mobile/Containers/Data/Application/30025787-C4F5-4F23-A345-C38EE44A180B/Documents/E678B939-730D-40D8-84EC-DE79FE4BDE3D.m4a

Note that i try to re-open the file like this:

self.audioRecorder = try AVAudioRecorder(url: self.recordedFileURL!, settings: settings)
self.audioRecorder.delegate = self
self.audioRecorder.isMeteringEnabled = true
let prepared = self.audioRecorder.prepareToRecord()
print("prepared to record = \(prepared)") // this returns FALSE when i am trying to re-open. But returns TRUE when i record for the first time.

When i try to open the file with the AVAudioPlayer i get error:

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

The code to open the file using the saved URL is this:

if (self.recordedFileURL != nil) {
  do {
    self.audioPlayer = try AVAudioPlayer(contentsOf: self.recordedFileURL!)
    self.audioPlayer.delegate = self
    self.audioPlayer.volume = 1.0
    self.audioPlayer.prepareToPlay()
  }
  catch let error {
    print("Play audio error: " + error.localizedDescription)
  }
}

* Updated *

I am beginning to think the file is actually not saved in the documents folder. I see this error in the output window:

fatal error: unexpectedly found nil while unwrapping an Optional value
2016-11-07 09:38:28.177535 fatal error: unexpectedly found nil while unwrapping an Optional value

How to verify this file exists in the documents folder?

* Updated *

I suspect I am saving the file to a temporary location. I will need to investigate in this direction once I get home. Here is a reference.

* updated 2 *

I found a better more precise reference here.

* RESOLVED *

I believe there were a couple (2) issues.

1) I made sure that I'm not using the path name of a file that already exists. If I use an existing path (which is the path to a recorded audio) to initialize the Recorder, it will override whatever audio was recorded. I had to make sure to use a separate/unique path for any new recording.

2) I had to ensure that I am working with the right URL paths. I found out that I wasn't consistent with this. In short, use the absolute string path, and use that consistently everywhere (reading & reading).

Community
  • 1
  • 1
AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
  • When i record i use category AVAudioSessionCategoryRecord. When i play it back i use AVAudioSessionCategoryPlayback. Again, the recording & playback works when i first create a recording. But if i were to open an existing URL (saved in the documents folder), this is when i am unable to open/record/playback. – AlvinfromDiaspar Nov 07 '16 at 17:50

0 Answers0