I’m new to Addressables and I’m looking for info on how I’d go about loading audioclips at runtime.
I have an AudioManager class that handles playing clips via an addressable URL or an assetreference of an audioclip.
My main question is when/how do I handle releasing/unloading the audioclip properly?
With the example code below the audio all plays properly when called, but it’s a bit confusing because I’m releasing the obj at the same time.
Still looking for info on when it’s an appropriate time to release an audioclip. Right away, when the clip is done playing? Some other way? And what’s the difference.
Pretty sure it’s not going to release if it’s still being used. After all “release” should reclaim the memory and if it’s being used that’s impossible. Hopefully someone will answer soon.
Well it looked promising, but I found that even with his exact example - I profiled it, and the audio memory indeed does not get released as he claims, at least according to the profiler. Not on the audio or memory profiler. I tried even going GC.Collect and yielding Resources.UnloadUnusedAssets. It worked 1 time but usually not.
So I’m still stuck on this. Hopefully someone from Unity will see this thread and tell us the right way to do it. Or that it’s broken (bug)?
I had a brief Skype with the author of that post, from the Netherlands, and we figured it all out. The Profiler running in the Editor isn’t so great and accurate, and I had to build the Addressable group in the Editor, but finally we got it to show properly. If you Profile on a device you’ll see it works properly.
I’m very confused about Addressable.Release. I can play audioclip even it has been released.
e.g.:
var clip = await Addressables.LoadAssetAsync(addressOfAudioClip).Task;
audioSource.clip = clip;
Addressables.Release(clip);
audioSource.Play();
I’m very confused about Addressable.Release. I can play audioclip even it has been released.
e.g.:
var clip = await Addressables.LoadAssetAsync(addressOfAudioClip).Task;
audioSource.clip = clip;
Addressables.Release(clip);
audioSource.Play();
correction: after many experiments,I found audioSource can’t play after releasing audioclip if set play mode script to using exsting build.
You don’t need to release in edit mode is my understanding, just like Resources.Load.
If you’re asking about during Play mode, it’s quite complicated. Not a couroutine, but when the audio source is stopped, we remove it from “active instances” and if that’s the last instance of that clip we unload it from Addressable. Basically you need to track the active instances yourself. There’s nothing built it that you can query.
I ended up remembering every runtime loaded audio clip, it’s audio source and Addressables loading handle. At the end of the turn (turn based game) I check list which stores these infos and if audio source is null, doesn’t play or play different clip (name comparison) I release audio clip from memory (nulling clip and Release stored handle).
But, even if Addressables Event Viewer shows clip is unloaded, it still resides in memory and I have no idea what to do with it. Clip is not part of any Asset bundle. I also tried to change settings for Addressables Group where I have audio clips to Pack separately if it may help. But no luck.
It was the same in built app. But i figured it out at last. Problem was that one audio clip was part of the same Addressables Group and was referenced in preloaded particle effect. And if one asset in Asset bundle (Group) is still referenced, nothing else from bundle can be unloaded. It’s tricky
Wooh!!! Help me understand that. I have an Addressable group with over 1GB of music in it. I am releasing the handles after a song finishes (and after setting the clip of the audio source to null). I always have on song playing from the group. The memory only releases if I Stop all music. So you are saying as long as I am playing music the memory will go up. I guess I could break the songs into different groups. I am shocked if this is the behavior and sounds like a bug! Is this true for all types of assets?
From what I’ve red and tried it works like that. Addressable groups are basically asset bundles and if anything from asset bundle is still in use it cannot be unloaded from memory. Specifically for music I separated all my tracks into several Groups for different game situations. So it should not hold too much of them in the memory.
Anyway, in Groups settings there is an option called Bundle Mode and you can select “Pack separately”. I suppose this should alter this behaviour. But for some reason it didn’t work for me when I tried several months ago. Something like I cannot even build the game, not sure. Please let me know if you will investigate it
Thanks Michal. Afraid to touch anything now that I have it working. If I hit stop, it releases the clip and handle and immediately cleans up the memory. Trying to have everything cross-fade thus must be the problem as one soundtrack is always playing. I will try having two groups and cross-fading between them. Right now no real controls on the playtrack selection in my games, just random. Will research “Pack separately” in my spare time.
Sorry to necro this post, but I’m having the same issue and I want to know if anyone find a better way, and if not if you can share more insight on how you’re doing this.
I’m always amazed that there’s no event that tells you if a clip has finish playing. Now on top of solving that I also need to get a hold of the addressable handle and keep it store?.. and also, if I have a bunch of gui sounds, just using one will load everyone into memory? Sorry if I sound repetitive, but after one year and a half, have you guys found more info on this? Sounds like using a coroutine for sounds will be better, although it does sound a bit like a mess.