- Home /
Unload audio from external source
I'm able to load audio from an external directory by making use of the WWW class. The problem I'm having is that I can't get the audio to unload and this is causing memory leaks.
I am loading audio like this and then storing all audio files in a dictionary for reference:
newAudioSource = gameObject.AddComponent<AudioSource>();
newAudioClip = new WWW("file:///" + absolutePath).GetAudioClip(false, false);
if (newAudioClip != null)
{
newAudioSource.clip = newAudioClip;
audioDict[audioKey] = newAudioSource;
}
and then I'm trying to unload the audio like this:
AudioSource sourceToRemove = audioDict[key];
audioDict.Remove(key);
Destroy(sourceToRemove);
But that's not working because I can see the that the audio memory is not going down in the profiler. Eventually the app crashes because of too much memory.
What happens if you set audioDict[key].clip = null; before destroying? If that doesn't work, try using System.GC.Collect() after the destroy, let me know how it goes.
Is newAudioSource and newAudioClip being stored anywhere else? Only thing I can think of is to grab sourceToRemove.clip, set the clip to null, and destroy the clip as well. $$anonymous$$aybe the AudioSource is storing the clip in a static reference array so that if any other AudioSource adds the same clip, it can use the same reference, or it could be making a single static deep copy. You might check if there is another programmatic way to remove the clip too.
Thanks for the replies guys. Unfortunately, none of the suggestions have worked.
I know that there are not any other references to these objects because I am able to make some modifications to the code to load the audio using the Resources directory and using Resources.load ins$$anonymous$$d of the WWW class to load. With that modification I am able to unload the audio memory with the originally posted code.
I wish I could load the audio using the Resources class but there is a 50$$anonymous$$B limit to the size of an Android app for the play store so I need to move the audio assets into an expansion file and so I need to load them using the WWW class.
What format are you using for the files? (mp3 would be one of the best formats to go for here)
Currently, I am using wav and mp3 files, though we are looking into compressing the wav files into mp3 as long as looping them doesn't cause any audio issues.
Answer by Bryarey · May 10, 2017 at 10:19 AM
Try to dosomething like: AudioClip clipToUnload = audioDict[key]; clipToUnload.UnloadAudioData(); Destroy(clipToUnload);
And after that remove your dictionary`s item and/or audio source, if need.
Your answer
Follow this Question
Related Questions
WWW.audioClip not playing on android build 1 Answer
Unity download audio 1 Answer
Find audio in android 0 Answers
loading/streaming audioclip using www class [android] 0 Answers
Why does sound on Android play at double speed and crackle? 3 Answers