Kawaii Ninja
Kawaii Ninja

Reputation: 1

audio clip time progress bar

 [Header("Status")]
    [Tooltip("To cheack current status")]
    bool isDragging;
    [Header("Time")]
    [Tooltip("to check the time")]
    float previousTime = 0;

    [Header("Components")]
    AudioSource _audio;
    TextMeshProUGUI _text;
    [SerializeField] Slider _slider;
    [SerializeField] ScrollRect scrollRect;
    public void ProgressBar(AudioSource audio, TextMeshProUGUI time)
    {
        if (_audio == null)
        {
            _audio = audio;
            _text = time;
        }
        UpdateUI();
    }

    private void UpdateUI()
    {
        try
        {
            if (_audio.clip != null)
            {
                if (IsAudioPlayingOrEnded() && !isDragging)
                {
                    {
                        // Update UI while audio is playing
                        float normalizedPosition = Mathf.InverseLerp(0, _audio.clip.length, _audio.time);
                        scrollRect.horizontalNormalizedPosition = normalizedPosition;
                        _slider.value = _audio.time;
                        _text.text = AudioFormatUtility.FormatLength(_audio.time, 2);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.Log("Error: " + e.ToString());
        }

    }

    public void OnPointerDown(PointerEventData eventData)
    {
        isDragging = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {

        isDragging = false;
    }

    public void OnSliderPlayBackValueChange()
    {
        if (_audio != null && _slider != null && isDragging)
        {
            _audio.time = _slider.value;
            float normalizedPosition = _slider.value / _audio.clip.length;
            scrollRect.horizontalNormalizedPosition = normalizedPosition;
            _text.text = AudioFormatUtility.FormatLength(_slider.value, 2);
        }
        UpdateUI();
    }

    public void OnScrollPlayBackValueChange()
    {
        if (_audio != null && scrollRect != null)
        {
            float value = Mathf.Abs(ReverseNormalizerValue(scrollRect.horizontalNormalizedPosition));
            _audio.time = value;
            _slider.value = value;
            _text.text = AudioFormatUtility.FormatLength(_slider.value, 2);
        }
        UpdateUI();
    }

    public float ReverseNormalizerValue(float normalizedValue)
    {
        float orginalValue = -_audio.clip.length;
        float reversedNormalizedValue = normalizedValue;
        float value = reversedNormalizedValue * orginalValue;
        return value;
    }

    public bool IsAudioPlayingOrEnded()
    {
        float currentTime = _audio.time;
        if (currentTime != 0)
        {
            if (currentTime != previousTime)
            {
                {
                    previousTime = _audio.time;
                    if (currentTime > _audio.clip.length)
                        _audio.time = _audio.clip.length;
                    return true;
                }
            }
        }
        previousTime = _audio.time;

        return false;
    }

when i play the audio it doesn't reach the end of the clip time for example if the clip length is 1:000 (1 sec) after i play this clip it will pause at 0:991 or 0:980 i want the audio clip to run full till 1:000 and pause there.

[ if i play the audio till the end time will reset to 0:000 i don't want that too ]

Upvotes: 0

Views: 17

Answers (0)

Related Questions