- Home /
Audio loops too early
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;
}
}
Tough to say. Switch statements are screwy. Good practice I learned was add in a Debug.Log one by one on each line between code to see when it was being executed. That might tell you if its being cut off at a certain point by something else or it could be your mp3 cutting off.
Agreed with GC1983. Put in a Debug statement on both and keep an eye out for it. I believe your goal is for this function to only be called once, so I'm curious to see if it is in fact being called twice.
I used Debug.Log and the code is working as intended, there isn't anything wrong with the clip itself either. I wonder if it could be to do with the "Load Type" setting?
Answer by Liens · Feb 24, 2013 at 08:33 AM
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.
Answer by David Kamunyu · Feb 22, 2013 at 10:29 AM
Hey,
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... :D
Switch statements and if statements pretty much do the same thing.