- Home /
How to script a MusicPlayer?
Hi
I have two tracks of music(an intro track and a loop track),i need and a script that plays the Intro track once(as the game starts) and following it(exactly after it's ending) I want the loop track to begin and fell in loop.
Can anybody help me with that?
Any helps would be appreciated!
Answer by sfc.itzhak · Jul 17, 2013 at 12:13 PM
here you go this is 1 way
AudioSource MpPlayer;
AudioClip FirstClip;
AudioClip SecondClip;
// Use this for initialization
void Start () {
MpPlayer.clip = FirstClip;
MpPlayer.loop = false;
MpPlayer.Play();
StartCoroutine(WaitForTrackTOend());
}
IEnumerator WaitForTrackTOend()
{
while (MpPlayer.isPlaying)
{
yield return new WaitForSeconds(0.01f);
}
MpPlayer.clip = FirstClip;
MpPlayer.loop = true ;
MpPlayer.Play();
}
edit shit should work now i forgot to call the second function
Tnx for helping ,this script plays the First clip But the second is not being Played(I also tried changing the firstClip to SecondClip in IEnumerator method but it doesn't working)
ahh i forgot somthing StartCoroutine(WaitForTrackTOend());
AudioSource $$anonymous$$pPlayer;
AudioClip FirstClip;
AudioClip SecondClip;
// Use this for initialization
void Start () {
$$anonymous$$pPlayer.clip = FirstClip;
$$anonymous$$pPlayer.loop = false;
$$anonymous$$pPlayer.Play();
StartCoroutine(WaitForTrackTOend());
}
IEnumerator WaitForTrackTOend()
{
while ($$anonymous$$pPlayer.isPlaying)
{
yield return new WaitForSeconds(0.01f);
}
$$anonymous$$pPlayer.clip = FirstClip;
$$anonymous$$pPlayer.loop = true ;
$$anonymous$$pPlayer.Play();
}
can you accept the answer so ppl will know there is answer to this question?
Answer by hajiyeve92 · Jul 14, 2017 at 10:37 AM
please follow link bellow and download asset. -best example for music player(almost everything included) https://www.assetstore.unity3d.com/en/#!/content/90589 Don't forget to leave your review
Your answer
Follow this Question
Related Questions
how to make the music better 1 Answer
Destroy "Don'tDestroyOnLoad" script in a specific scene ?? 3 Answers
Music trackers and iOS 0 Answers
adding music to the main menu screen 0 Answers
.wav file not working 2 Answers