- Home /
Streaming music from StreamingAssets folder
Hi all I have problem with streaming music from StreamingAssets folder. I need this for custom player soundtracks. It takes much memory.
I did some testing (10 ogg files - total 61,3MB) of uncompressed loading and streaming.
("Empty" means empty project with no audio and "Inside" means audio files inside Assets folder) Memory is changing when streaming around +- 20MB
Streaming from StreamingAssets (what I need) takes such memory. And there is another problem which other not have. When streaming music 1, play next music and then play music 1 again, it is not playing! Only silence.
I storing clips on array of AudioClip called clips. Here is my loading code
IEnumerator LoadAllClips(bool streaming)
{
//... more code
// Request to our file on HDD
WWW request = new WWW("file:///" + filePath);
yield return StartCoroutine(WaitForRequest(request));
// Get audio clip and rename it
AudioClip clip = request.GetAudioClip(true, streaming);
clip.name = fileName;
// Add to array
clips[i] = clip;
// ... more code
}
I can store only file paths and doing WWW requests runtime. But it will be more work and more complicated. Is there a better way?
(btw: forum gives me "Your content can not be submitted. error")
Answer by Xan1995 · Aug 09, 2016 at 05:27 PM
No reply? Unity community is well...
I found solution but it is complicated. I store only paths to files in array and doing WWW request before play required clip. (storing WWW requests not helped)
Music Player
void Play(string fileName)
{
// Unload audio clip
if (myAudioSource.clip != null)
{
myAudioSource.Stop();
AudioClip clip = myAudioSource.clip;
myAudioSource.clip = null;
clip.UnloadAudioData();
DestroyImmediate(clip, false); // This is important to avoid memory leak
}
// Load Clip then assign to audio source and play
manager.LoadClip(fileName, (clip) => { myAudioSource.clip = clip; myAudioSource.Play(); });
}
Music Manager
public void LoadClip(string fileName, Action<AudioClip> onLoadingCompleted)
{
StartCoroutine(LoadClipCoroutine("file:///" + fileName, onLoadingCompleted));
}
IEnumerator LoadClipCoroutine(string file, Action<AudioClip> onLoadingCompleted)
{
WWW request = new WWW(file);
yield return request;
if (onLoadingCompleted != null)
onLoadingCompleted(request.GetAudioClip(true, true));
request.Dispose();
}
If anybody have better solution, please let me know.
Your answer
Follow this Question
Related Questions
Stream audio from a YouTube playlist's URL? 0 Answers
VideoPlayer: No audio when playing from StreamingAssets 1 Answer
UnityWebRequestMultimedia.GetAudioClip dont't work in WebGL build (2019.3.15f1). 1 Answer
How would I go about using music from StreamingAssets folder (imported by the player) 0 Answers
How can I load an AudioClip from a local audio file using UnityWebRequests? 1 Answer