I have music (mp3’s) from an array being played as 2D AudioClips in an AudioSource. For some reason the music plays for a while then loops when the song is in the last quarter of playthrough and not even finished playing yet.
Does anyone know how to resolve this issue?
Here’s some code attached to the AudioSource playing the clips.
#pragma strict
enum MusicTracks { Sea, Land }
var music : AudioClip[];
function OnLevelWasLoaded () {
switch (Application.loadedLevelName) {
case "Land":
var newAudioClip : AudioClip = music[MusicTracks.Land];
if(audio.clip != newAudioClip) {
audio.clip = newAudioClip;
audio.Play();
}
break;
case "Sea": case "Sea_Under":
newAudioClip = music[MusicTracks.Sea];
if(audio.clip != newAudioClip) {
audio.clip = newAudioClip;
audio.Play();
}
break;
}
}
The MP3’s import settings, “Load Type” was set to “Compressed in memory”, once I set it to “Decompress on load” this did not happen any longer. I am unsure of why this makes a difference.
Edit: After reading the documentation, “Decompress on load” is not reccomended for large files, so I switched it to “Stream from disc”. It also seems to solve this problem.
From previous experience, I have learnt that it is a good idea to avoid case and while. Don’t get me wrong, I love swearing into the screen as much as the next guy when things aren’t working correctly, but IF has been a long time friend and companion and would always work well, so long as the checks are good.
So if you could make a check, like if (land){CODE} else if (sea || Sea_Under){code} and so on, you can even toss in a check that can ensure that the music plays only at a certain time… maybe by calling a function for music playing in {code part}, with an audio.loop = true; line. So one call and the rest you leave to the system.
I don’t want to change your code, but it could save you a headache
However, if you have some kind of superpowers and figure out how to make the CASE work, please x 20 share, because I’m also interested in how to make use of it…