VideoPlayer

2019-04-08  本文已影响0人  SmallFlydream
/**
 *Copyright(C) 2019 by #PROJECTNAME#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:      Nxf 
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:        #DATE#
 *Description:   
 *History:  
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using xxx;
using static UnityEngine.UI.Slider;

namespace Expm.VideoPalyer01
{
    public class VideoPlayerController : MonoBehaviour
    {

        #region Private Properties
        private int _clipHour, _clipMinute, _clipSecond;
        private int _currHour, _currMinute, _currSecond;
        private bool isPause = true;
        #endregion

        #region Public Properties
        public VideoPlayer _videoPlayer;
        public TextMesh _text;
        public Slider _slider;
        #endregion

        #region 拖拽事件
        private void ObjectTrigger_OnDragEndAction(Transform obj)
        {
            isPause = true;
        }

        private void ObjectTrigger_OnDragAction(Transform obj)
        {
            isPause = false;
            SkipToTrame(_slider.value);
        }
        #endregion

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="obj"></param>
        private void ObjectTrigger_OnClickAction(Transform obj)
        {

            if (obj == _slider.transform)
            {
                Debug.Log(obj.GetComponent<RectTransform>().rect.xMin);
                Debug.Log(obj.GetComponent<RectTransform>().rect.xMax);
                Vector2 localPoint;
                if (RectTransformUtility.ScreenPointToLocalPointInRectangle(obj.GetComponent<RectTransform>(), Input.mousePosition, null, out localPoint))
                {
                    Debug.LogWarning(localPoint.x);
                    float pct = Mathf.InverseLerp(obj.GetComponent<RectTransform>().rect.xMin, obj.GetComponent<RectTransform>().rect.xMax, localPoint.x);
                    SkipToTrame(pct);
                }
            }
        }

        #region Unity Life Cycle

        // Use this for initialization
        void Start()
        {
            GetVideoTime();
        }

        // Update is called once per frame
        void Update()
        {
            if (isPause)
            {
                _videoPlayer.Play();
                Debug.Log("ddddd");
                _currHour = GetTime(Time.Hour, (float)_videoPlayer.time);
                _currMinute = GetTime(Time.Minute, (float)_videoPlayer.time);
                _currSecond = GetTime(Time.Second, (float)_videoPlayer.time);
                _text.text = string.Format("{0:D2}:{1:D2}:{2:D2}/{3:D2}:{4:D2}:{5:D2}", _currHour, _currMinute, _currSecond, _clipHour, _clipMinute, _clipSecond);
                _slider.value = (float)(_videoPlayer.time / _videoPlayer.length);
            }
            else
            {
                _videoPlayer.Pause();
            }

        }

        #endregion

        #region Private Methods
        /// <summary>
        /// 初始化
        /// </summary>
        private void GetVideoTime()
        {
            _clipHour = GetTime(Time.Hour, (float)_videoPlayer.length);
            _clipMinute = GetTime(Time.Minute, (float)_videoPlayer.length);
            _clipSecond = GetTime(Time.Second, (float)_videoPlayer.length);
            ObjectTrigger.OnClickAction += ObjectTrigger_OnClickAction;
            ObjectTrigger.OnDragAction += ObjectTrigger_OnDragAction;
            ObjectTrigger.OnDragEndAction += ObjectTrigger_OnDragEndAction;
        }
        /// <summary>
        /// 点击的比例
        /// </summary>
        /// <param name="pct"></param>
        private void SkipToTrame(float pct)
        {
            var frame = _videoPlayer.frameCount * pct;
            _videoPlayer.frame = (long)frame;

        }
        /// <summary>
        /// 转换时间
        /// </summary>
        /// <param name="time"></param>
        /// <param name="timeLength"></param>
        /// <returns></returns>
        private int GetTime(Time time, float timeLength)
        {
            int getTime = 0;
            switch (time)
            {
                case Time.Hour:
                    getTime = (int)(timeLength / 3600);
                    break;
                case Time.Minute:
                    getTime = (int)(timeLength - ((int)(timeLength / 3600)) * 3600) / 60;
                    break;
                case Time.Second:
                    getTime = (int)(timeLength - ((int)(timeLength - ((int)(timeLength / 3600)) * 3600) / 60) * 60 - (int)(timeLength / 3600) * 3600);
                    break;
                default:
                    break;
            }
            return getTime;
        }

        #endregion
    }
    public enum Time
    {
        Hour,
        Minute,
        Second,
    }
}

20190409_152500.gif
上一篇下一篇

猜你喜欢

热点阅读