Audio Cutting Out Unexplainably
I'm trying to implement a 15-song soundtrack to play in my game. I have a script that randomly chooses the next song to play when the song currently playing is finished. It works for the first few songs during the playing of the game, but then it starts to act weird. The audio will cut out more and more frequently as it plays, skipping to the next song. The part where it skips in the songs is always different and Unity recognizes the song's length correctly, so the problem isn't there. I converted my files from .mp3 to .ogg as suggested on other posts and there was no change in the cutting of tracks. I changed "audio.Play()" to "audio.PlayOneShot()". I also messed with the settings a bunch, yet nothing seems to help. The songs are the only audio clips being played in the game, so they shouldn't be overloading the system or anything either. In stats, right as the audio skips and goes to the next song, there is a visible increase in db, from "-11 db" to "-80 db." Is my audio messed up? Is it my audio playing script? Is it just Unity? Any help would be greatly appreciated.
Here's my script:
#pragma strict
public var Pre : AudioClip;
public var Con : AudioClip;
public var Sky : AudioClip;
public var Fig : AudioClip;
public var The : AudioClip;
public var Imp : AudioClip;
public var Dis : AudioClip;
public var Conn : AudioClip;
public var Hard : AudioClip;
public var End : AudioClip;
public var Rea : AudioClip;
public var Sta : AudioClip;
private var SoundSource : AudioSource;
public var Goi : AudioClip;
public var Emp : AudioClip;
public var Clo : AudioClip;
var playNow: boolean = false;
function Awake() { DontDestroyOnLoad(gameObject);
SoundSource = gameObject.AddComponent(AudioSource);
SoundSource.playOnAwake = false;
SoundSource.rolloffMode = AudioRolloffMode.Logarithmic;
SoundSource.loop = false;
SoundSource.priority = 0;
}
var randomplay : float;
function Start() {
randomplay = Random.Range(1,16);
audioer();
}
function audioer(){
if (randomplay == 1) {
SoundSource.clip = Pre;
SoundSource.PlayOneShot(Pre);
yield WaitForSeconds(Pre.length);
Start();
}
if (randomplay == 2) {
SoundSource.clip = Con;
SoundSource.PlayOneShot(Con);
yield WaitForSeconds(Con.length);
Start();
}
if (randomplay == 3) {
SoundSource.clip = Sky;
SoundSource.PlayOneShot(Sky);
yield WaitForSeconds(Sky.length);
Start();
}
if (randomplay == 4) {
SoundSource.clip = Fig;
SoundSource.PlayOneShot(Fig);
yield WaitForSeconds(Fig.length);
Start();
}
if (randomplay == 5) {
SoundSource.clip = The;
SoundSource.PlayOneShot(The);
yield WaitForSeconds(The.length);
Start();
}
if (randomplay == 6) {
SoundSource.clip = Imp;
SoundSource.PlayOneShot(Imp);
yield WaitForSeconds(Imp.length);
Start();
}
if (randomplay == 7) {
SoundSource.clip = Dis;
SoundSource.PlayOneShot(Dis);
yield WaitForSeconds(Dis.length);
Start();
}
if (randomplay == 8) {
SoundSource.clip = Conn;
SoundSource.PlayOneShot(Conn);
yield WaitForSeconds(Conn.length);
Start();
}
if (randomplay == 9) {
SoundSource.clip = Hard;
SoundSource.PlayOneShot(Hard);
yield WaitForSeconds(Hard.length);
Start();
}
if (randomplay == 10) {
SoundSource.clip = End;
SoundSource.PlayOneShot(End);
yield WaitForSeconds(End.length);
Start();
}
if (randomplay == 11) {
SoundSource.clip = Rea;
SoundSource.PlayOneShot(Rea);
yield WaitForSeconds(Rea.length);
Start();
}
if (randomplay == 12) {
SoundSource.clip = Sta;
SoundSource.PlayOneShot(Sta);
yield WaitForSeconds(Sta.length);
Start();
}
if (randomplay == 13) {
SoundSource.clip = Goi;
SoundSource.PlayOneShot(Goi);
yield WaitForSeconds(Goi.length);
Start();
}
if (randomplay == 14) {
SoundSource.clip = Emp;
SoundSource.PlayOneShot(Emp);
yield WaitForSeconds(Emp.length);
Start();
}
if (randomplay == 15) {
SoundSource.clip = Clo;
SoundSource.PlayOneShot(Clo);
yield WaitForSeconds(Clo.length);
Start();
}
}
Answer by MadDevil · Oct 08, 2015 at 05:06 AM
I cant find anything wrong with your code, but I would suggest you use switch case instead of so many if statements.
and don't forcefully call start method as inbuilt unity methods sometimes messes up the execution order of the code if called forcefully.
@$$anonymous$$adDevil , Thanks for replying! I tried both of your suggestions. Neither of them add any improvement to the skipping of the soundtrack. What should I do?
Your answer
Follow this Question
Related Questions
Audio is way too soft on mobile but alright in Unity Editor! 0 Answers
Need help: new sound isnt played correctly anymore 2 Answers
Cracking at end of audio? 0 Answers
Second AudioClip won't play 0 Answers
Second AudioClip won't play 0 Answers