Challenge: Recorded sound doesn't play on subsequent run


#1

I’ve got the challenge working, thanks to some Googling to find the right settings for the AVAudioRecorder. When recording, I save to a file named recording.ima4 file in the Documents directory. I can then play it back, too.

However, if I quit the app and relaunch, then click the playback button for the sound I recorded, no sound comes out and my button keeps displaying “Stop playback”. I set a couple of breakpoints and was able to confirm that an AVAudioPlayer is created looking in the right place, but the -audioPlayerDidFinishPlaying:successfully: delegate method is never reached. What’s so special about trying to play the recording from my previous run vs. playing a recording made during my current run? Any clues appreciated…


#2

I figured it out. I wasn’t just relaunching, I was deleting the app from the simulator. So of course its Documents directory and contents went with it. So when I launched again, a new Documents directory was created which is empty until I make a recording. I was trying to play a file which didn’t yet exist, so the AVAudioPlayer was in an undefined state–not playing, and not stopped. I fixed it by checking for actual file existence at the path before setting up the AVAudioPlayer. It seems so obvious now. But I would’ve thought the AVAudioPlayer initialization would fail if there’s no file at the given NSURL.