1
votes

AVPlayer does not play a .aif file recorded with AVAudioRecorder. My current process is:

  1. AVAudioSession.sharedInstance's category is set to AVAudioSessionCategoryPlayAndRecord
  2. AVAudioRecorder is instantiated with a NSURL in the app's documents directory and settings of format of kAudioFormatAppleIMA4, bit rate of 32000, 1 channel, and sample rate of 16000.0.
  3. Audio is recorded, then stopped. The file is saved and I can find it in the app's documents directory and verify the audio is properly recorded.
  4. An instance of AVPlayer is instantiated with the file's NSURL. An observer is added to this object to register changes to its status. When the AVPlayer is ReadyToPlay, I call the play function.

However, despite control flow reaching the point of AVPlayerStatus.ReadyToPlay and calling play, no sound is produced. I'm checking errors and verifying the existence and attributes of the file at the NSURL. I've also tried following the process of this SO post by instantiating an AVAsset, AVPlayerItem, and AVPlayer. No luck.

Any thoughts on why AVPlayer isn't playing audio for this newly recorded local file?

edit: The app is built for iOS 9 with Xcode 7 beta 5. The app is able to play audio files streamed with AVPlayer.

1
I'm having a very similar issue and when I tried to playback on simulator it worked. The playback is NOT working on device however and I cannot figure out why. There are no errors on encoding or decoding of the audio file.C0D3

1 Answers

2
votes

The issue was not caused by AVAudioRecorder or AVPlayer. My recording URL is a NSURL I'll call fileURL. I passed this NSURL to a different view controller but AVPlayer wouldn't play when instantiated with fileURL. I got a hint to what was going wrong when

fileURL.checkResourceIsReachableAndReturnError(&error)

failed with the error "The file does not exist." Instead, I instantiated a new NSURL with the path of the newly recorded file using:

let url = NSURL(fileURLWithPath: path)

With this url, I was able to instantiate and play an instance of AVPlayer.