- Home /
Download an audio file in background without lags.
Hello, I have a problem with loading an audio file from HDD, more precisely while it loads, the game stops until it is fully downloaded even if I use yield's or other coroutine methods. Am I doing something wrong or it has to be done using streaming? (which I once used but want to avoid it this time, too much work and code involved)
Here's the code am using right now:
@HideInInspector var musicFolderContents : String[];
@HideInInspector var customPlaylistSwitch : int[];
private var musicPlaylist : int[];
private var actualMusicID : int;
private var externalMusicScan : int;
private var bufferedAudioclip : AudioClip;
private var HDDaudioFile : WWW;
var playNextTrack : boolean;
var actualAudioTrack : String;
function MusicPlayer (){
if (playNextTrack){
bufferedAudioclip = null;
// Play music from internal library
if (musicPlaylist[actualMusicID] < originalMusic.length){
bufferedAudioclip = originalMusic[musicPlaylist[actualMusicID]];
} else{
// Play music from external folder
actualAudioTrack = Path.GetFileNameWithoutExtension("file://"+musicFolderContents[musicPlaylist[actualMusicID]-originalMusic.length]);
if (Application.isEditor) HDDaudioFile = new WWW ("file://"+musicFolderContents[musicPlaylist[actualMusicID]-originalMusic.length]);
else HDDaudioFile = new WWW ("file://"+Application.dataPath+"/Music/"+musicFolderContents[musicPlaylist[actualMusicID]-originalMusic.length]);
bufferedAudioclip = HDDaudioFile.GetAudioClip(false, false);
}
actualMusicID = (actualMusicID + 1) % musicPlaylist.length;
playNextTrack = false;
}
if (bufferedAudioclip != null){
if (musicPlaylist[actualMusicID] < originalMusic.length){
audio.clip = bufferedAudioclip;
audio.Play();
bufferedAudioclip = null;
} else{
if (HDDaudioFile.isDone){
if (bufferedAudioclip.isReadyToPlay){
audio.clip = bufferedAudioclip;
audio.Play();
bufferedAudioclip = null;
}
}
}
}
}
Are you sure it's downloading that blocks your app? I suggest adding print("before")
before yield, print("after")
after yield, and print("update")
inside your Update
function. There should be at least a few "update" texts logged to the console between "before" and "after". That means your program is not blocked.
Btw - there's no need to check www.isDone
- if coroutine resumed, then isDone
should be true.
Well, tried to check where the lag occurs and... no idea! I can't find place where prints would appear like: one before lag and one after, they always appear together... I altered the code in my question so can u try to point out where the loading might make the game stop for a moment?
Ok sooo I discovered that loading the file and assigning it to AudioSource does not create the stop, what does create it is the audio.Play(); in this case... sooooooooo, what can be done about this? EDIT: Correction, audio.clip.isReadyToPlay cause it... dafuq, $$anonymous$$dblow and whatever
Hmm - I've just read a forum topic related to such freezes, and it looks like this might be related to file decompression. You might check the thread for more information.
hmmmm, from all those posts I found in there and links they posted it seems I need to use "strea$$anonymous$$g" method afterall... oh well, thanks for helping me figure out what causes the problem :)