- Home /
Play next track when song is finished playing
im trying to make a "radio" player where music is endlessly playing when you have the project open. I have a pause, play and skip track button which work perfectly the only issue i have is when the track has finished playing it won't skip to the next track and id have to manually do it via skip button. i've tried googling this issue as i kept facing error codes. Trying to fix this has been hurting my brain and i really need help on this from more experienced coders. Also dont bully me for my screen name :) This is my code below:
public class RadioController : MonoBehaviour { [Header("List of Tracks")] [SerializeField] private Track[] audioTracks; private int trackIndex;
[Header("Text UI")]
[SerializeField] private Text trackTextUI;
private AudioSource radioAudioSource;
private void Start()
{
radioAudioSource = GetComponent<AudioSource>();
trackIndex = 0;
radioAudioSource.clip = audioTracks[trackIndex].trackAudioClip;
trackTextUI.text = audioTracks[trackIndex].name;
}
public void SkipForwardButton()
{
if (trackIndex < audioTracks.Length - 1)
{
trackIndex++;
UpdateTrack(trackIndex);
PlayAudio();
}
}
void UpdateTrack(int index)
{
radioAudioSource.clip = audioTracks[index].trackAudioClip;
trackTextUI.text = audioTracks[index].name;
{
trackIndex++;
UpdateTrack(trackIndex);
PlayAudio();
}
}
public void AudioVolume(float volume)
{
radioAudioSource.volume = volume;
}
public void PlayAudio()
{
radioAudioSource.Play();
}
public void PauseAudio()
{
radioAudioSource.Pause();
}
}
Answer by Elian71 · Apr 27 at 01:59 AM
void Start()
{
radioAudioSource = GetComponent<AudioSource>();
trackIndex = 0;
radioAudioSource.clip = audioTracks[trackIndex].trackAudioClip;
trackTextUI.text = audioTracks[trackIndex].name;
UpdateTrack(trackIndex);
PlayAudio();
StartCoroutine(VerifyPlaying());
}
IEnumerator VerifyPlaying(){
while (true)
{
yield return new WaitForSeconds(1f);
if (!radioAudioSource.isPlaying)
SkipForwardButton();
}
}
void UpdateTrack(int index)
{
radioAudioSource.clip = audioTracks[index].trackAudioClip;
radioAudioSource.time = 0;
trackTextUI.text = audioTracks[index].name;
// {
// trackIndex++;
// UpdateTrack(trackIndex);
// PlayAudio();
// }
}
You can verify if an audiosource is playing an audio with a Coroutine that executes every second. Once the audio finished you change to the next track or you can do it in the update as the documentation: https://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html
Also you could make an Invoke("YourFunction", radioAudioSource.clip.length) to skip the track when finish Additional info:
radioAudioSource.time: allows you get and set the actual time of a track in seconds.
radioAudioSource.clip.length: returns the lenght of the clip in seconds.
Your answer
Follow this Question
Related Questions
Audio Source will not "unmute" after toggling? 2 Answers
How To trim audio run time in game? 1 Answer
C sharp cant play Audio 2 Answers
Stream music in Unity mobile 0 Answers
Audio Questions 0 Answers