Any way to code skipping song after pressing key on keyboard ?
Hello, I'm a beginner in coding in Unity and I need help in implementing skipping song after pressing selected key, because I tried to do it but without any luck. For now I have only music player that plays all songs I put in it. What I want is that after I press selected key, the next song plays and after reaching the end of playlist it goes back to the first song. Thanks in advance for all answers. Here's the script for playing music:
public class MusicPlayer : MonoBehaviour
{
public AudioSource audioSource;
public AudioClip[] soundtrack;
int i = 0;
void Start ()
{
StartCoroutine(PlayMusic());
}
IEnumerator PlayMusic ()
{
yield return null;
for (; i < soundtrack.Length; i++)
{
audioSource.clip = soundtrack[i];
audioSource.Play();
while (audioSource.isPlaying)
{
yield return null;
}
}
}
}
Answer by tormentoarmagedoom · Jun 06, 2018 at 11:10 AM
Good day.
first, you need a list of clips to be played. when the clip is finished, the variable audioSource.isPlaying will become false, so you can detect it and change the clip to be played.
You can also use Input.Keycode to change the clip when user click a specific button or key.
Bye!
Your answer
Follow this Question
Related Questions
Stopping Audio When Zoomed In/Out 0 Answers
Why does AudioSource.time return Infinity in Unity 5.3? 2 Answers
Don't reload the AudioSource when back to scene 1 Answer
Precisely timestamping when audio leaves speaker 0 Answers
AudioSource repeatedly called 0 Answers