0

Function to load audio in LoadFile.cs:

public async Task<AudioClip> LoadAudioFile(string path) // Loads *.mp3's
{
    //Load audio from the chosen *.mp3 file
    using UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.MPEG);
    var operation = www.SendWebRequest();

    while (!operation.isDone)
        await Task.Yield();

    if (www.result == UnityWebRequest.Result.Success)
        return DownloadHandlerAudioClip.GetContent(www);
    else
        return null;
}

Calling the function in TrackLoader.cs:

beatmap.music = await LoadFile.instance.LoadAudioFile(Path.Combine(folderPath, beatmap.musicName));

Users are able to create their own custom levels in the rhythm game I'm developing, meaning they can import their own audio, art for the song, etc. I load audio with the above method (and essentially the same way with images).

Loading a 4 minute *.mp3 takes around 300-450ms on my good computer when I run this. In the game, loading the songs when I enter the song select scene can take up minutes which is far too long. I'm not sure how to implement a better solution.

Games such as osu!mania and Quaver have instant access to the music, and the best guess I have is the music is being streamed instead of loaded all at once then played.

I attempted to Google how to do this but I'm stumped. How can I speed up this process?

If there is anything I missed I will be happy to provide anything at all.

Reason for needing this: To be able to preview songs in song select.

VIDEO SHOWING HOW SLOW LOADING CURRENTLY IS

I understand there may be design flaws with this such as not background loading before switching to the song select scene, loading what is clicked, etc. However, even with those implemented I believe it won't achieve the quickness I need such as how Quaver has it. I don't know why the FPS is so low and why the debugger keeps going even after exiting play mode.

Ramin Amiri
  • 151
  • 1
  • 3
  • 12
  • Well I'd say before a user actually clicks on one of these you do **not** want to load anything at all ... you just want to **list** available file locations (evtl meta data etc) but not actually load the entire audio clip. You start loading/streaming the audio data itself once a user has selected one ... Also why use a `Task` instead of a Coroutine? – derHugo Nov 22 '21 at 09:33
  • @derHugo The issue is that, when the user has selected one, it takes far too long for the audio to begin playing (300-450ms). Games like Quaver do it instantly and at any point in the song. I used a Task because it allowed me to call the function from a singleton called LoadFile.cs in multiple scripts since I need to load audio in multiple scenes. I tested with Coroutines and they were the same time – Ramin Amiri Nov 22 '21 at 09:56
  • why I ask is: Unity is not thread-safe and using Tasks you never know on which thread the Task finishes. With a Coroutine you know that it is only pocessed within the Unity main thread `Update` routine – derHugo Nov 22 '21 at 10:11
  • @derHugo I've switched to Coroutines, but unfortunately by main issue still remains--it takes around 500ms now. – Ramin Amiri Nov 23 '21 at 00:29

0 Answers0