简体   繁体   English

滑块移动不顺畅。 (Unity3d)

[英]Slider isn't moving smoothly. (Unity3d)

I have an audio clip and a slider.我有一个音频剪辑和一个滑块。 The slider is acting as a progress bar (Timeline) for the audio clip.滑块充当音频剪辑的进度条(时间轴)。 I can pause and play the audio also I can skip the track as well.我可以暂停和播放音频,也可以跳过曲目。 Everything is working fine.一切正常。 The problem is slider isn't moving smoothly it's kinda jittery.问题是滑块移动不顺畅,有点紧张。 Please edit it if somebody can.如果有人可以,请编辑它。 Thanks in advance.提前致谢。

Here is the code:这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MusicPlayer : MonoBehaviour
{
    AudioSource audioSource;
    Slider slider;
    public AudioClip track;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        slider = GetComponent<Slider>();

        audioSource.clip = track;
        audioSource.Play();

        slider.minValue = 0;
        slider.maxValue = track.length;
    }

    // Update is called once per frame
    void Update()
    {
        slider.value = audioSource.time;
        
    } 

    public void MovePoition()
    {
        audioSource.time = slider.value;
    }

    public void play()
    {
        audioSource.Play();
    }

    public void pause()
    {
        audioSource.Pause();
    }

}

On Update , ensure smoothness by using Time.deltaTime to update the slider's value if it is playing.Update ,通过使用Time.deltaTime更新滑块的值(如果它正在播放)来确保平滑度。 Otherwise, and also in play and pause , re-sync the position with the playtime.否则,也在playpause ,将位置与播放时间重新同步。

However, you need to avoid the callback when setting the value.但是,您需要在设置值时避免回调。 Create an empty event at Start and assign it to the slider when updating the value.Start创建一个空事件并在更新值时将其分配给滑块。

Finally, add a flag to keep track of if the track should be playing.最后,添加一个标志来跟踪是否应该播放曲目。 This prevents the source from stopping when the end of the clip is played then the slider is dragged.这可以防止源在播放剪辑的结尾然后拖动滑块时停止。

After setting the source time in MovePoition , depending on the compression of the clip, it might not be able to set to the exact value the slider has.MovePoition设置源时间后,根据剪辑的压缩,它可能无法设置为滑块具有的确切值。 So, you should re-update the slider value with the time that the audio source decides to use.因此,您应该根据音频源决定使用的时间重新更新滑块值。

Altogether:共:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MusicPlayer : MonoBehaviour
{
    AudioSource audioSource;
    Slider slider;
    Slider.SliderEvent emptyEvent;
    public AudioClip track;
    private bool isPaused;
    private bool needSync;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        slider = GetComponent<Slider>();

        audioSource.clip = track;
        audioSource.Play();

        slider.minValue = 0;
        slider.maxValue = track.length;

        emptyEvent = new Slider.SliderEvent();
    }

    void SetSliderWithoutCallback(float time) 
    {
        Slider.SliderEvent temp = slider.onValueChanged;
        slider.onValueChanged = emptyEvent;
        slider.value = time;
        slider.onValueChanged = temp;
    }

    // Update is called once per frame
    void Update()
    {
        if (audioSource.isPlaying) 
        {
            float newTime = Mathf.Min(slider.value + Time.deltaTime, slider.maxValue);
            SetSliderWithoutCallback(newTime);
        } 
        else if (!isPaused)
        {
            SetSliderWithoutCallback(slider.maxValue);
            isPaused = true;
        }
    } 

    public void MovePoition()
    {
        audioSource.time = slider.value;
        if (!isPaused) 
        {
            audioSource.Play();
            SetSliderWithoutCallback(audioSource.time);
        }
    }

    public void play()
    {
        audioSource.Play();
        isPaused = false;
        SetSliderWithoutCallback(audioSource.time);
    }

    public void pause()
    {
        isPaused = true;
        audioSource.Pause();
        SetSliderWithoutCallback(audioSource.time);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM