Home > OS >  Use a Slider to show the progress of an Audiosource when play is pressed in Unity
Use a Slider to show the progress of an Audiosource when play is pressed in Unity

Time:12-18

I'm building an app where the user clicks on a button and an audio source plays.. There is the option to pause and exit.

I've tried to integrate a slider bar to show progress..

{
    [Header("List of Tracks")]
    [SerializeField] private Track[] audioTracks;
    
    private int trackIndex; 
    
    [Header("Text UI")]
    [SerializeField] private TextMeshProUGUI trackTextUI;
    
    private AudioSource audio;
            
    // Start is called before the first frame update
    private void Start()
    {
        audio = GetComponent<AudioSource>();
        
        audio.clip = audioTracks[trackIndex].trackAudioClip;
        
        trackTextUI.text = audioTracks[trackIndex].name;
                
    }
    
    public void FiveMinuteMed()
    {
        trackIndex = 0;
        audio.Play();
    }
    
    public void TenMinuteMed()
    {
        trackIndex = 1;
        audio.Play();   
        
    }
    
    public void FifteenMinuteMed()
    {
        trackIndex = 2;
        audio.Play();
    }

  public void PlayAudio()
  {
      audio.Play();
  }
  
  public void PauseAudio()
  {
      audio.Pause();
  }
  
   public void StopAudio()
  {
      audio.Stop();
  }
  
 }

I tried to add the following snippet, into the on Update() function.. obviously declaring the variables etc on start. And when I click play.. The app just freezes..

 public Slider time;
public AudioSource audio;
// Start is called before the first frame update
void Start()

    {
        audio = GetComponent<AudioSource>();
    }
    
    // Update is called once per frame
    void Update()
    {
        time.maxValue = audio.clip.length;
        time.value = audio.time;
        audio.time = time.value;
    }

Any idea on how I can bring the slider in within the original code snippet above.. ?

CodePudding user response:

Remove this line and see if it works:

audio.time = time.value;

CodePudding user response:

You can use the UI Toolkit for Unity. ProgressBar will be used in this scenario. One way is given below:

public AudioSource source;
public float progress;
public UIDocument document;

VisualElement root;
ProgressBar progressBar;

void Awake()
{
    // getting the root visual element
    root = document.rootVisualElement;
    // getting the progress bar
    progressBar = root.Q<ProgressBar>("progressBar");
}

void Start()
{
    // setting up the title of the progress bar
    progressBar.title = source.clip.name;
}

void Update()
{
    // calculating the progress as percent because
    // the min and max values of the progress bar read-only
    progress = source.time / source.clip.length * 100;
    progressBar.value = progress;

    // debug to check the progress
    Debug.Log(progress   "%"   " out of "   source.clip.length   " seconds");
}

I have tested the code and its works properly. If you want a Slider you can easily convert this code into a one supporting Slider.

Just change the type from ProgressBar to Slider.

  • Related