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…!!

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!

its working , but i cant move the slider the in order to chage the position of the track…

Its been a while from this issue, but was struggling with the same and found a solution.
We need two scripts:

  1. This first one is going to be attached to the slider, and its used to know when the user is dragging the slider.
using UnityEngine;
using UnityEngine.EventSystems;

public class CheckOnPointerDown : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public bool onPointerDown = false;
    public void OnPointerDown(PointerEventData eventData)
    {
        onPointerDown = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        onPointerDown = false;
    }
}
  1. Second script is going to be our main script where we control anything else (any other logic that you want). We also want to reference the previous script in this second one.
[SerializeField] private AudioSource _audioSource;
[SerializeField] Slider _slider;
[SerializeField] TextMeshProUGUI _sliderTime;
[SerializeField] CheckOnPointerDown _checkOnPointerDown;
public void Update()
{
    if (_audioSource.isPlaying )
    {
        float time = _audioSource.time;
        int minutes = Mathf.FloorToInt(time / 60F);
        int seconds = Mathf.FloorToInt(time - minutes * 60);
        string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
        _sliderTime.text = niceTime;

        if (!_checkOnPointerDown.onPointerDown)
        {
            _slider.value = time / _audioSource.clip.length;
        }
        else
        {
            _audioSource.time = _slider.value * _audioSource.clip.length;
        }
    }
}

I also added a reference to show a timer but feel free to delate if you don’t need that.