Unity

Unity VideoPlayer

2018-08-11  本文已影响1139人  玄策丶

VideoPlayer控制脚本,使用videoplayer组件控制视频频播放

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

public class VideoCtrl : MonoBehaviour {
    public static VideoCtrl instance 
    private VideoPlayer vPlayer;

    public GameObject videoImage;//播放Image
    private Button Kaishi,Zanting, BtnReStart;//开始,暂停,重播
    public Slider sliderVideo;//进度条
    private Button BtnX;//关闭
    private Image VideoPanel; //视频背景
    private Text NowTime;//播放时间
    private Text ZongTime;//总时间

    private float tt;  //视频总时长
    private float Index_t; //进度条计时时间

    private float shi,fen,miao;

    private bool IsPlay = true;
    // Use this for initialization
    void Awake () {
        instance =this;
        vPlayer = videoImage.GetComponent<VideoPlayer>();
        Kaishi = GameObject.Find("Kaishi").GetComponent<Button>();
        Zanting = GameObject.Find("Zanting").GetComponent<Button>();
        BtnReStart = GameObject.Find("ReWindButton").GetComponent<Button>();
        sliderVideo = GameObject.Find("SliderVideo").GetComponent<Slider>();
        VideoPanel = GameObject.Find("VideoPanel").GetComponent<Image>();
        BtnX = GameObject.Find("CloseButton").GetComponent<Button>();
        ZongTime= GameObject.Find("ZongTimeText").GetComponent<Text>();
        NowTime = GameObject.Find("NowTimeText").GetComponent<Text>();
    }

    public void OnEnable()
    {
        BtnReStart.onClick.AddListener(ClickReStart);
        BtnX.onClick.AddListener(ClickBtnX);
        Kaishi.onClick.AddListener(ClickKaishi);
        Zanting.onClick.AddListener(ClickZanting);
        //改变进度条
        //sliderVideo.onValueChanged.AddListener(delegate { ChangeVideo(sliderVideo.value); });
    }
    void Start()
    {
        ClickKaishi();//是否自动播放

        //初始化总时间
        //获取视频总时长    帧数/帧速率=总时长    如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长
        //tt = vPlayer.frameCount / vPlayer.frameRate;
        tt = (float)vPlayer.clip.length;

        //赋值slider的MaxValue
        sliderVideo.maxValue = tt;

        fen = (int)tt / 60;
        miao = (int)tt % 60;
        ZongTime.text = string.Format("/ {0:D2} : {1:D2}  ",
            fen.ToString(), miao.ToString());

    }
    public void OnDisable()
    {
        BtnReStart.onClick.RemoveListener(ClickReStart);
        BtnX.onClick.RemoveListener(ClickBtnX);
        Kaishi.onClick.RemoveListener(ClickKaishi);
        Zanting.onClick.RemoveListener(ClickZanting);
        //sliderVideo.onValueChanged.RemoveListener(delegate { ChangeVideo(sliderVideo.value); });
    }

    // Update is called once per frame
    void Update ()
    {
        //播放
        if (IsPlay)
        {
            vPlayer.Play();
            //Time.timeScale = 1;
            //进度条前进
            Index_t += Time.deltaTime;
            if (Index_t >= 0.1f)
            {
                sliderVideo.value += 0.1f;              
                Index_t = 0;
            }
        }
        else
        {
            vPlayer.Pause();
            //Time.timeScale = 0;
        }
        //进度条到底停止播放
        if (sliderVideo.maxValue - sliderVideo.value <= 0.1f)
        {
            // sliderVideo.value = sliderVideo.maxValue;//不循环1
            //ClickZanting();//不循环2
            ClickReStart();//loop
        }
        
        //播放时间显示,每帧执行
        ChangeTime((float)vPlayer.time);
    }


    //改变视频进度
    public void ChangeVideo(float value)
    {
        vPlayer.time = value;

    }
    //播放时间显示
    void ChangeTime(float value)
    {       
        fen = (int)value / 60;
        miao = (int)value % 60;
        NowTime.text = string.Format("{0:D2} : {1:D2}  ",
            fen.ToString(), miao.ToString());
    }

    //重播按钮
    public void ClickReStart()
    {
        sliderVideo.value = 0;
        vPlayer.time = 0;
    }
    //关闭按钮
    public void ClickBtnX()
    {
        VideoPanel.gameObject.SetActive(false);
    }
    //开始按钮
    public  void ClickKaishi()
    {
        if(sliderVideo.value == sliderVideo.maxValue){
            sliderVideo.value = 0;
        }
        videoImage.SetActive(true);
        IsPlay = true;
        Zanting.gameObject.SetActive(true);
        Kaishi.gameObject.SetActive(false);
    }
    //暂停按钮
    public void ClickZanting()
    {
        IsPlay = false;
        Zanting.gameObject.SetActive(false);
        Kaishi.gameObject.SetActive(true);
    }
}

Slider控制脚本,使用进度条控制视频进度,挂在给Slider

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class SliderChange : MonoBehaviour, IDragHandler,IPointerClickHandler
{
    //拖动改变视频进度
    public void OnDrag(PointerEventData eventData)
    {
        VideoCtrl.instance.ChangeVideo(VideoCtrl.instance.sliderVideo.value);
    }
    //点击改变视频进度
    public void OnPointerClick(PointerEventData eventData)
    {
        VideoCtrl.instance.ChangeVideo(VideoCtrl.instance.sliderVideo.value);
    }
}
上一篇下一篇

猜你喜欢

热点阅读