unity AVpro插件视频进度条添加

public MediaPlayer _mediaPlayer;
    public Slider _videoSeekSlider;
    public Text time;
    float _setVideoSeekSliderValue;
    private bool _wasPlayingOnScrub;

    void Update()
    {

        int _all = (int)float.Parse((_mediaPlayer.m_Info.GetDurationMs() * 0.001f).ToString("F1"));
        TimeSpan allTime = new TimeSpan(0, 0, _all);

        int _current = (int)float.Parse((_mediaPlayer.m_Control.GetCurrentTimeMs() * 0.001f).ToString("F1"));
        TimeSpan currentTime = new TimeSpan(0, 0, _current);

        time.text = currentTime.Hours + ":" + currentTime.Minutes + ":" + currentTime.Seconds + "/" + allTime.Hours + ":" + allTime.Minutes + ":" + allTime.Seconds;
        if (_mediaPlayer && _mediaPlayer.Info != null && _mediaPlayer.Info.GetDurationMs() > 0f)
        {
            float time = _mediaPlayer.Control.GetCurrentTimeMs();
            float d = time / _mediaPlayer.Info.GetDurationMs();
            _setVideoSeekSliderValue = d;
            _videoSeekSlider.value = d;
        }
    }

    public void OnVideoSeekSlider()
    {
        if (_mediaPlayer && _videoSeekSlider && _videoSeekSlider.value != _setVideoSeekSliderValue)
        {
            _mediaPlayer.Control.Seek(_videoSeekSlider.value * _mediaPlayer.Info.GetDurationMs());
        }
    }


    public void OnVideoSliderBegin()
    {
        if (_mediaPlayer)
        {
            _wasPlayingOnScrub = _mediaPlayer.Control.IsPlaying();
            if (_wasPlayingOnScrub)
            {
                _mediaPlayer.Control.Pause();

            }
            OnVideoSeekSlider();
        }
    }

    public void OnVideoSliderEnd()
    {
        if (_mediaPlayer && _wasPlayingOnScrub)
        {
            _mediaPlayer.Control.Play();
            _wasPlayingOnScrub = false;

        }
    }

posted @ 2021-06-22 15:05  All_Nighter  阅读(653)  评论(0编辑  收藏  举报