Unity VideoPlayer 滑动条控制播放进度,滑动过程可显示滑动画面

8 篇文章 2 订阅

unity的VideoPlayer其实足够了,不用插件也可以很好的干活。

按照我的步骤来一趟即可。本文主要给一个可以实现滑动的基础逻辑,可在此基础上做扩展。

先看看效果

代码块:

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

/// <summary>
/// 滑动条控制视频播放进度,可静帧或者滑动播放
/// </summary>
public class PlayVideo : MonoBehaviour
{
    public VideoClip videoClip;         // 视频的文件 参数

    public MySlider videoTimeSlider;      // 视频的时间 Slider

    //定义参数获取VideoPlayer组件和RawImage组件

    internal VideoPlayer videoPlayer;

    private RawImage rawImage;

    // Use this for initialization

    void Start()
    {

        //获取场景中对应的组件

        videoPlayer = this.GetComponent<VideoPlayer>();

        rawImage = this.GetComponent<RawImage>();

        videoPlayer.clip = videoClip;

        //videoNameText.text = videoClip.name;

        clipHour = (int)videoPlayer.clip.length / 3600;

        clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60;

        clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60);

        videoPlayer.Play();


       

        videoTimeSlider.OnChangeValue += OnChangeValue;

        videoPlayer.sendFrameReadyEvents = true;//激活frameReady的必要选项
        videoPlayer.frameReady += VideoPlayer_frameReady;
    }

    private bool _isReady = false;

    private bool _isDrag = false;
    private void VideoPlayer_frameReady(VideoPlayer source, long frameIdx)
    {
        _isReady = true;
    }


    private void OnChangeValue(float value,bool isDrag)
    {
        _isReady = false;
        _isDrag = isDrag;
        float oldVal;
        if (isDrag)
        {
            oldVal = (float)videoPlayer.time / (float)videoPlayer.clip.length;

           // Debug.Log("oldVal is:" + oldVal + "    value is:" + value);
            if (Math.Abs(oldVal - value) < 0.055f) return;
        }
      
        videoPlayer.time = value * (float)videoPlayer.clip.length;

        //以下两种方法可二选一
        
        //滑动的时候定帧
        if (_isDrag)
            videoPlayer.Pause();
        else videoPlayer.Play();


        //滑动的时候,自动播放
        //videoPlayer.Play();
    }

    // Update is called once per frame

    void Update()
    {
        
        if (videoPlayer.texture == null)
        {
            return;
        }
        if(videoPlayer.texture!=rawImage.texture)
          rawImage.texture = videoPlayer.texture;

        ShowVideoTime();

    }

    /// <summary>
    /// 显示当前视频的时间
    /// </summary>

    private void ShowVideoTime()
    {
        // 当前的视频播放时间

        currentHour = (int)videoPlayer.time / 3600;

        currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;

        currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60);

        // 把当前视频播放的时间显示在 Text 上

        //videoTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2} / {3:D2}:{4:D2}:{5:D2}", currentHour, currentMinute, currentSecond, clipHour, clipMinute, clipSecond);

        // 把当前视频播放的时间比例赋值到 Slider 上
        if (_isReady && !_isDrag)
        {
           
            videoTimeSlider.SetValue((float)(videoPlayer.time / videoPlayer.clip.length));
        }

    }

   

    // 当前视频的总时间值和当前播放时间值的参数

    private int currentHour;

    private int currentMinute;

    private int currentSecond;

    private int clipHour;

    private int clipMinute;

    private int clipSecond;

}
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


/// <summary>
/// 滑动位置,预览该位置的视频定帧
/// </summary>
public class MySlider : Slider
{

    public Action<float,bool> OnChangeValue;

    private bool _isDrag = false;

    private Coroutine _coroutine;

    private WaitForSeconds _waitForSeconds;

    protected override void Awake()
    {
        base.Awake();
        _waitForSeconds=new WaitForSeconds(0.2f);
    }
    
    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);

        _coroutine = StartCoroutine(Wait());
        _isDrag = true;
    }

    public override void OnPointerUp(PointerEventData eventData)
    {

        base.OnPointerUp(eventData);
        _isDrag = false;
        if (_coroutine != null) StopCoroutine(_coroutine);
        _coroutine = null;
        if (OnChangeValue != null)
            OnChangeValue(this.value, _isDrag);
    }
   
    public void SetValue(float newValue)
    {
        if (_isDrag) return;
        this.value = newValue;
    }

    private IEnumerator Wait()
    {
        while (true)
        {
            yield return _waitForSeconds;
            if (OnChangeValue != null)
                OnChangeValue(this.value, _isDrag);

        }
    }
}

步骤一:

 步骤二:

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值