- Home /
Change to another song when song is finish playing
Hello, is there is a way to change another music or tune? ie. when music 1 finish playing a song then music 2 plays then 3 then 4 and so on? i have search this everywhere but i couldn't locate it
Answer by AlucardJay · Jul 09, 2013 at 07:03 AM
Ok, so this one can be achieved by using the length of the audio clip and Invoke.
http://docs.unity3d.com/Documentation/ScriptReference/AudioClip-length.html
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html
So the pattern would be : Play the audioClip, then Invoke a function at a time delay of audio.clip.length, that function assigns a new audioClip and repeats the process. Example pseudocode
audio.clip = clip1;
audio.Play();
Invoke( "PlayNextTrack", audio.clip.length );
// function PlayNextTrack
audio.Stop(); // just in case
audio.clip = clip2; // this would actually be choosing the next song in an array, after checking if the index is out of range then going back to the first track in the array
audio.Play();
Invoke( "PlayNextTrack", audio.clip.length );
Here is exact an code from my project with Invoke method.
private void Start()
{
AudioListener.volume = PlayerPrefs.GetFloat("volumeSound", 1.0f);
music.ignoreListenerVolume = true;
music.volume = PlayerPrefs.GetFloat("volumeMusic", 1.0f);
if (music.volume > 0.0f)
{
PlayMusic();
}
}
void PlayMusic()
{
music.clip = musics[Random.Range(0, musics.Length - 1)];
music.Play();
Invoke(nameof(PlayMusic), music.clip.length);
}
Answer by flamy · Jul 09, 2013 at 07:16 AM
public string[] _clipNames; //Keep all audio files inside resources folder and give the value of path alone in this varible through editor.
int i=0;
void Start()
{
StartAudio(); // start first time without Delay
}
void StartAudio()
{
audio.clip = Resources.Load (_clipNames[i]) as AudioClip;
audio.Play();
i++;
if(i>=_clipNames.Length)
i=0;
Invoke("StartAudio",audio.clip.Length+0.5f); //0.5f is the delay given after a song is over.
}
Ok here these are the thinks that has to be noted. First I have used _clipNames array to save path of the audio clips that are kept in resources folder. I could have attached all the clips to the Editor by declaring AudioClip array instead of string but it doing that would make the clips stay in memory until the script instance is destroyed.. For more info on Resources folder check this page.
Next I have used Invoke function... with the delay as the length the currently playing audio clip. This way the next audio will be changed automatically once the audio clip is over.
Hope this is clear.
I'm a little disappointed you have given the same answer, but also provided code. Who cares, right, I don't need the karma. But don't you think there's enough there in my answer to get the ball rolling? And am I justified in feeling a little plagiarized?
Also, any response from the OP will indicate their level of knowledge.
Sure, I write code for people too, but a good guide is : If they provide code, fix and/or provide code; If they provide an example, give an example.
Also, your Invoke is correct. The name of the method as a string :
function Invoke (methodName : String, time : float) : void
i was typing it for ages... and completed it jus now... didnt knew you have answered already =/
Changing the invoke..
Ok, no problem then. It was a simultaneous post. Sorry if the above comments offended you, I totally understand now.
Edit : It was a simultaneous post (not duplicate like I wrote first). You have always been one of the good guys (that's why I was initially surprised), UA needs people like you !