#YouTube Slider

1 messages Β· Page 1 of 1 (latest)

kind fulcrum
#

Actually I would like to do smth similar to YouTube Slider. The video's time is changed just after dragging.
Does it makes sense with if (!Input.GetMouseButton(0)) ?

#

YouTube Slider

dim cedar
#

That would make sense to me - though I may not be the best sounding board based on my limited experience πŸ˜…

kind fulcrum
#

who knows

#

actually

dim cedar
#

I think one potential complication to either approach would be if you intend to support keyboard input for the slider

kind fulcrum
#

this code does not work

timeSlider.onValueChanged.AddListener(_ =>
{
    if (!Input.GetMouseButtonDown(0))
    {
        Debug.Log("changed");
        audioSource.time = timeSlider.value;
    }
});
kind fulcrum
dim cedar
#

I think it would have to be Input.GetMouseButton(0) there, as GetMouseButtonDown() is tied to the frame

kind fulcrum
dim cedar
#

hmmm

#

I'll play around a bit myself - I'm curious

#

Ah right - because the value won't change after the mouse button is released πŸ˜…

kind fulcrum
#

I guess I can try to do smth with OnMouseButtonUp

dim cedar
#

@kind fulcrum this seems to work fairly well, in very limited testing:

public class YouTubeSlider : MonoBehaviour, IEndDragHandler, IBeginDragHandler
{
    public Slider timeSlider;
    public AudioSource audioSource;

    private float previousValue;

    public void OnEndDrag(PointerEventData eventData)
    {
        if (timeSlider.value == previousValue)
            return;

        audioSource.time = timeSlider.value;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        previousValue = timeSlider.value;
    }
}
#

I have it attached to the slider GO

kind fulcrum
#

wait, I gonna try it

#

also the solution I have just made:

timeSlider.onValueChanged.AddListener(_ =>
{
    StartCoroutine(ChangeTime());

    IEnumerator ChangeTime()
    {
        yield return new WaitUntil(() => !Input.GetMouseButton(0));

        Debug.Log("worked? haha");
        audioSource.time = timeSlider.value;
    }
});
dim cedar
#

Ah very cool! I did not know you could WaitUntil() single-frame inputs like that πŸ—’οΈ

kind fulcrum
#

but wait

#
public void OnEndDrag(PointerEventData eventData)
{
    if (timeSlider.value == previousValue) return;

    audioSource.time = timeSlider.value;
}

public void OnBeginDrag(PointerEventData eventData)
{
    previousValue = timeSlider.value;
}
#

why should it work on timeSlider?

dim cedar
#

You mean why attached to like the slider GO instead of the handle?

kind fulcrum
#

so it should be attached to the slider?

#

that makes sense then

dim cedar
#

I did anyway - I figured it would be nice to work with those events whether they are triggered on the handle, or the background/bar thingy. That way it sort of automatically accounts for the case of starting the drag at a random position on the bar

kind fulcrum
#

actually

#

I have no let the previous code and it works, so the music gets skipped when dragging the slider

#

but

#

i cannot drag it physically when I change the slider's value in Update

#
timeSlider.value = (int)audioSource.time;
#

I just cannot drag it

#
private void Start()
{
    timeSlider.onValueChanged.AddListener(_ =>
    {
        Debug.Log("onValueChanged");

        StartCoroutine(ChangeTime());

        IEnumerator ChangeTime()
        {
            yield return new WaitUntil(() => !Input.GetMouseButton(0));

            Debug.Log("worked? haha");
            audioSource.time = timeSlider.value;
        }
    });
}

private void Update()
{
    timeSlider.value = (int)audioSource.time;
    timeInput.text = $"{ParseMusicTime(audioSource.time)} / {musicParsedTime}";
}
#

I guess I should use additional bool ??

dim cedar
#

I think that's the key - yeah

#

was just writing that πŸ‘

kind fulcrum
#

haha

#

nice

#

I gonna try that

dim cedar
#

Doesn't that code start a big pile of ChangeTime() coroutines running in parallel?

dim cedar
#

Every time onValueChanged() triggers, a new coroutine is started - I would think when you release left mouse you'd get a Debug.Log("worked? haha"); for each of the coroutines which was started?

kind fulcrum
#

oh, let me see it in debug now

#

yeah

#

it does

dim cedar
#

But I guess that could also be mitigated with the new bool 😁

kind fulcrum
#

haha yeah, lemme try this to

#

that's nice issue though

#

and this is big minus for game's productivity

dim cedar
#

lmao

kind fulcrum
#

I guess I even know why

#
timeSlider.onValueChanged.AddListener(_ =>
{
    changingTime = true;

    if (canStartChangeTimeCoroutine) StartCoroutine(ChangeTime());

    IEnumerator ChangeTime()
    {
        canStartChangeTimeCoroutine = false;

        yield return new WaitUntil(() => !Input.GetMouseButton(0));

        Debug.Log("worked? haha");

        audioSource.time = timeSlider.value;

        changingTime = false;
    }
});
kind fulcrum
#

oh

dim cedar
#

The only suspicious thing I see there is canStartChangeTimeCoroutine is never reset to true.

dim cedar
#

But I think changingTime could also fulfill canStartChangeTimeCoroutine's purpose:

timeSlider.onValueChanged.AddListener(_ =>
{
    if (!changingTime) StartCoroutine(ChangeTime());

    IEnumerator ChangeTime()
    {
        changingTime = true;

        yield return new WaitUntil(() => !Input.GetMouseButton(0));

        Debug.Log("worked? haha");

        audioSource.time = timeSlider.value;

        changingTime = false;
    }
});
kind fulcrum
#

yeah, that works, thank you

#

I have also thought about it, but was too stupid to implement

dim cedar
#

😁 that's half of my workday

kind fulcrum
#

@dim cedar thank you for your time and help today, I really do appreciate it

dim cedar
#

Np - I like a good problem :)