- Home /
AudioSource.clip.time won't work?
Unity seems to be telling me that AudioSource.clip.time doesn't exist? Does it? I know AudioSource.time does and I have AudioSource.clip.length working so why can't I get AudioSource.clip.time to work?
I am trying to make an audiosource shuffle between songs. At the moment it can tell when the first song has ended by using the AudioSource.time but the problem is that after that it can't tell when the next song has ended unless there was a way to get the AudioSource.clip.time. Any help would be super helpful because this is driving me insane!
using UnityEngine;
using System.Collections;
public class Juke : MonoBehaviour
{
public float time;
public AudioClip[] whopperBeats;
public AudioSource radioStation;
public int randomclipNumber;
public int lastTrack;
// Use this for initialization
void Start()
{
Tunes();
lastTrack = 481516;
}
void Tunes()
{
randomclipNumber = Random.Range(0, whopperBeats.Length);
radioStation.clip = whopperBeats[randomclipNumber];
radioStation.Play();
}
void Update ()
{
lastTrack = 0 + randomclipNumber;
if (randomclipNumber == lastTrack)
{
//Tunes();
print("Re-Shuffle Initiated");
}
if (radioStation.clip.time >= radioStation.clip.length -1)
{
print("Play a new tasty beat.");
//Tunes();
}
}
}
As the clip can be used in multiple AudioSources at the same time, it cannot have a value for the current position.
AudioSource.time returns the time of the current clip. I don't think you need to reset anything.
AudioSource.time and AudioClip.length work perfectly to show the current music track progress for me.
Answer by Vuzok · Jul 20, 2016 at 01:43 AM
Ok so for anyone running into a similar issue in the future I didn't find a way to use AudioSource.clip.time and I don't think that it exists. I ended up just resetting AudioSource.time back to zero each time a clip finished playing.
Answer by Arkaid · Jul 20, 2016 at 01:34 AM
Because it's AudioSource.time, not AudioSource.clip.time
Yeah I know that but thats not what I am looking for. I don't think you read the question fully. AudioSource.time gives you the time that the AudioSource has been playing for. What my question asked was how to get the time that the current audio clip has been playing for- and no I can't use AudioClip.length because when you try use that on an audio array it returns with the value of the array ins$$anonymous$$d of the length of the clip. Like I said in the question I did already use AudioSource.time but thats not what I am looking for. Thanks for the response anyway I found a workaround.