How to control Music track with the UI slider
Hie I am Creating a music player in unity 3D 5.6.2 and I am Stuck on how to make the current playing track match the value of the UI slider to control the Music Any help will be appreciated Please Reply Soon..!!
Answer by Jawchewa · Aug 10, 2017 at 06:46 PM
You probably will want to create a new script that contains methods that look something like this(C#):
public AudioSource audioSource;
public Slider slider;
public void ChangeAudioTime()
{
audioSource.time = audioSource.clip.length * slider.value;
}
public void Update()
{
slider.value = audioSource.time / audioSource.clip.length;
}
Attach this script to a game object and setup the audio source and slider values to point to your respective game objects. Then, in the slider game object, hook up the on value change event to point to the ChangeAudioTime method that we just defined. This should allow you to drag the slider to adjust the current position in the song. I also added the update to constantly set the value of the slider to the current position in the song.
You might also need to write some additional logic to make it so the song doesn't play while you are dragging the slider, but this should help you get started. Good Luck!
Hie I think I did not defined it properly , So Basically when I click a button a song is been played and the music player starts and now I need to take that song which is playing and make the track slider/UI slider move accordingly to the track .... and Like this there are several button and several tracks but only one UI Slider So can you please help me
Answer by MaxQuinonesSantander · Dec 06, 2021 at 10:11 AM
its working , but i cant move the slider the in order to chage the position of the track..
Your answer
Follow this Question
Related Questions
Unity 5 - My slider handle isnt aligned with my slider 0 Answers
Slider.value doesn't show the value in real-time but after the execution of the script 1 Answer
How can I check if there is no free space in the Text component? 1 Answer
Number string to .text returning 0 0 Answers
Changing the UI Text 0 Answers