3

If an app requires hundreds of audio files (they're small, 5-25KB), is it better to put them in an asset catalog or the documents directory? I've tried both approaches and they don't seem much different, it's a pain to manage hundreds of files in the project navigator either way and it's the same app filesize for both.

Steve
  • 5,802
  • 12
  • 54
  • 76
  • Are these files you wish to include with your app so they are in place when the user first installs your app? Or are these files that your app downloads when the user first runs your app? – rmaddy Dec 13 '16 at 18:49
  • 1
    Are you sure you mean the documents directory? That is a read/write directory that's created, empty, the first time the app is launched. The assets catalog is read-only, and it's contents are stored in the app bundle. If these sound files ship with the app then you would have to copy them out of the bundle and into the documents directory if that's where you wanted to keep them. – Duncan C Dec 13 '16 at 18:49
  • @rmaddy, the files are included w/the app. – Steve Dec 13 '16 at 19:05
  • @DuncanC, I'm not sure I understand, I was able to play the audio files from the catalog on the iPhone, so I assume a copy of the files are already with the app. If you copy files from the bundle to the documents directory, isn't that duplication? – Steve Dec 13 '16 at 19:08
  • 1
    That's my point. You can't ship an app with contents already in the documents directory. If you want your sound files in your documents directory you would have to either download them or copy them from somewhere in your bundle (asset catalog or a folder in the bundle). That would mean the sound files would be in 2 places. Thus I don't think it makes sense to say "should I store my sound files in an asset catalog or in my documents directory" because they are not equivalent. – Duncan C Dec 13 '16 at 19:16
  • @DuncanC, ah I see my mistake, see my comment to rmaddy's answer. Thanks. – Steve Dec 13 '16 at 19:18

2 Answers2

5

Since you wish to bundle your audio files with your app, you can't use the Documents folder. The use of the Documents folder is a runtime feature than can only be used once your app has been installed from the App Store and run by the user.

To include the files with your app so they exist immediately, they must be part of your app's resource bundle. Whether you use an asset catalog or simply store them in the bundle is up to you, but it's not possible to package them in the Documents folder since there is no Documents folder until the first time the app is run by the user.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • My mistake, for some reason, I thought when I copied the audio files over to the project navigator, that created the files in the documents directory. It's part of the bundle. So is there a difference between in the bundle and in the catalog? – Steve Dec 13 '16 at 19:16
4

So far, I've found one thing that influences whether to "just" put audio files in the app bundle or to put it in the asset catalog portion of the app bundle.

When you put audio files in the asset catalog, it's available as data, there's no URL. As I understand it now, if you want a URL, you'll have to extract the data and create a file, which is double work.

When you have a URL that's available automatically when you "just" put audio files in the app bundle, it gives you more options. Like using AVQueuePlayer with AVPlayerItem. AVPlayerItem only takes a URL, not data. If you wanted to play audio files one after another from the asset catalog, it's more work, you have to write code for AVAudioPlayerDelegate, whereas with AVQueuePlayer, you just provide an array of AVPlayerItems.

I suspect that there may be more advantages having a URL to reference and am not seeing the advantages the asset catalog has yet, so my opinion is subject to change.

Steve
  • 5,802
  • 12
  • 54
  • 76