5

I have a bunch of audio files which I keep in my asset catalog to benefit from the store optimizations that it brings. However, the AVAudioPlayer ideally wants AVURLAssets from which subsequently AVPlayerItems can be created.

Problem is, asset data doesn't have an URL. How do I combine asset catalog assets with AVPlayer?

Apple advises loading the files via NSDataAsset constructor, which doesn't help.

hoshy
  • 481
  • 6
  • 15
  • In what way doesn't it help? NSDataAsset is the only way to access audio files kept in the asset catalog, so it's not as if there's any issue here. – matt Apr 05 '17 at 18:41
  • 1
    Because the asset doesn't provide a URL which in turn doesn't let me use AVURLAssets which in turn doesn't let me use AVPlayerItems which in turn doesn't let me have properties like 'duration' and methods like 'seek'. – hoshy Apr 05 '17 at 18:43
  • That's right. So obviously you either have to keep the audio files in your app bundle instead of the asset catalog, or, if you do keep them in the asset catalog initially, you have to write them out to disk in some place where they can have a URL (Documents folder, temp folder, whatever). – matt Apr 05 '17 at 18:47
  • And what's the downvote for? Please elaborate. – hoshy Apr 05 '17 at 18:47
  • 1
    Yeah, obviously. It just kinda collides with the idea to encourage using asset catalogs to load resources on demand. Would be decent to have both advantages. – hoshy Apr 05 '17 at 18:48
  • As you say, obviously. I don't get what you mean about "using asset catalogs to load resources on demand", or what "advantages" or "optimizations" you think would be lost by making this easier on yourself. A file's a file. – matt Apr 05 '17 at 18:50
  • https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AppThinning/AppThinning.html – hoshy Apr 05 '17 at 18:51
  • Are you referring to app thinning? Are these sound files actually subject to thinning — that is, do you have one sound file on iPad and another on iPhone, or some similar distinction? – matt Apr 05 '17 at 18:52
  • Yes, they are localized. And it makes a difference if you download 80 or 160 megabyte. – hoshy Apr 05 '17 at 18:53
  • Then you'll have to copy them from the asset catalog onto disk where they can have a URL, as we already agreed. – matt Apr 05 '17 at 18:56
  • Yeah, that's just horribly hacky. – hoshy Apr 05 '17 at 18:58
  • Don't blame me. :) File an enhancement request with Apple. I mean, if your point is that it's stupid that the asset catalog can't hand you a legitimate file URL when you know perfectly well that the file in the asset catalog _is_ on disk somewhere, you have a fair point. But that won't change the facts. – matt Apr 05 '17 at 19:04
  • Also, I wonder whether your app might benefit from use of ODR (on-demand resources). This would allow the files to be kept "in" the app bundle while keeping the initial app download size small. – matt Apr 05 '17 at 19:05
  • 1
    Not blaming you, I was just wondering whether I overlooked something or somebody comes up with a better idea. That curiosity legitimizes the question. Thanks for your effort though. – hoshy Apr 05 '17 at 19:06
  • Yeah looking into that right now, thanks. – hoshy Apr 05 '17 at 19:07
  • I found a solution in this thread https://stackoverflow.com/a/69298570/15042552 – SwiftApprentice Sep 23 '21 at 10:52

0 Answers0