One of my staff requested a music player made in Unity so that he could test his songs (he says that they loop perfectly in Audacity, but he’s heard from me that they don’t loop right in Unity). So I am making one. I’ll eventually need it to change an attached audio clip with one selected off of the computer, but for now I’m having a different problem:
var attachedAudio : AudioClip;
var looping = false;
var playbackBar : float = 0.0;
var timeSinceStart = 0.0;
var refreshTime = 0;
function Start () {
CountSeconds ();
}
function OnGUI () {
GUILayout.Label(timeSinceStart + " / " + attachedAudio.length + " seconds");
GUILayout.BeginHorizontal();
if (GUILayout.Button("Play")) {
audio.clip = attachedAudio;
audio.Play();
refreshTime = 1;
}
if (GUILayout.Button("Pause")) {
audio.clip = attachedAudio;
audio.Pause();
refreshTime = 0;
}
if (GUILayout.Button("Stop")) {
audio.clip = attachedAudio;
audio.Stop();
refreshTime = 0;
timeSinceStart = 0.0;
}
GUILayout.EndHorizontal();
playbackBar = GUILayout.HorizontalSlider (timeSinceStart, 0.0, attachedAudio.length);
looping = GUILayout.Toggle (looping, "Loop");
if (looping == true) {
audio.loop = true;
} else {
audio.loop = false;
}
}
function CountSeconds () {
while (timeSinceStart < attachedAudio.length) {
timeSinceStart ++;
yield WaitForSeconds (refreshTime);
}
}
I want to make the slider move with the music. It isn’t draggable, so I don’t have to worry about changing what part of the song is playing (which I think Unity can’t do with AudioSources). The problem is that when I want it to stop moving up, I use set the refreshTime variable to zero. Well, duh. If its waiting no seconds it’ll just zip right to the end of the playback bar. So how do I set refreshTime to not happen at all? Thanks!