Skip to content

Instantly share code, notes, and snippets.

@emilylaguna
Created June 13, 2023 17:02
Show Gist options
  • Save emilylaguna/72286e3326b6678750608aba28d6632c to your computer and use it in GitHub Desktop.
Save emilylaguna/72286e3326b6678750608aba28d6632c to your computer and use it in GitHub Desktop.
let fileURL = URL(fileURLWithPath: strongSelf.episodePath!)
do {
strongSelf.audioFile = try AVAudioFile(forReading: fileURL, commonFormat: AVAudioCommonFormat.pcmFormatFloat32, interleaved: false)
// AVAudioFile.length is an expensive operation (often in the seconds) so here we attempt to load a cached value instead
strongSelf.cachedFrameCount = DataManager.sharedManager.findFrameCount(episode: episode)
if strongSelf.cachedFrameCount == 0 {
// we haven't cached a frame count for this episode, do that now
strongSelf.cachedFrameCount = strongSelf.audioFile!.length
DataManager.sharedManager.saveFrameCount(episode: episode, frameCount: strongSelf.cachedFrameCount)
// If the audio file length is still 0 then way may not be able to read the file correctly
if strongSelf.cachedFrameCount == 0 {
throw PlaybackError.unableToOpenFile
}
}
} catch {
objc_sync_exit(strongSelf.playerLock)
PlaybackManager.shared.playbackDidFail(logMessage: error.localizedDescription, userMessage: nil)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment