What is the best practice for embedding and accessing audio files in an iOS app, circa iOS 11 / Swift 4? I’m a little unclear on the exact implementations and their limitations.
It looks like I have two options, packaging the audio assets as data sets inside of an asset catalog, or simply including the files in a folder in my project and ensuring they are included in the app build target so they get bundled.
One additional requirement in my case - the app needs to playback various pre-packaged audio files offline sans controller UI, both forwards (rate 1.0) and backwards (rate -1.0).
The rub is accessing the audio data through the various AVFoundation classes. AVAudioPlayer can play audio data directly crafted from NSDataAsset derived from a named asset catalog asset, but it cannot play audio at negative rates (aka backwards). AVPlayer can playback an AVPlayerItem that canPlayReverse, but all the AVPlayer and AVPlayerItem initialization methods require URLs which are not supported by asset catalogs as part of their .car compilation obfuscation. I cannot find an equivalent of the UIImage imageNamed:inBundle: method in the audio domain to craft audio objects from asset catalog data set data. The Bundle urlForResources methods don’t do the trick.
My premature conclusion is that I should just bundle the audio in a folder in the app, outside of any asset catalog, for flexibility with the more robust AVPlayer classes. However, putting embedded assets in an app outside of an assets catalog feels wrong.