关于VideoPlayer的使用(方便自己查看的笔记)

unity5.6之后加的一个videoPlayer组件,也是查资料查到的方便自己之后查看,直接用代码记录

using UnityEngine;
using UnityEngine.Video;
public static class VideoPlayController
{
    //获取视频总时长
    public static int GetVideoTimeCount(this VideoPlayer vp)
    {
        return (int)(vp.frameCount / vp.frameRate);
    }
    ///


    /// 获取视频进度
    ///

    ///
    ///
    public static float GetVideoProgression(this VideoPlayer vp)
    {
        return (float)((vp.time * vp.frameRate)/(vp.frameCount / vp.frameRate));
    }
 
    ///
    /// 设置视频进度
    ///

    ///
    ///
    public static void SetVideoProgression(this VideoPlayer vp, float progression)
    {
        float time = (int)vp.frameCount / vp.frameRate * progression;
        vp.time = time;
        vp.Play();
    }
}

测试 在Unity中视频的播放及用进度条控制视频的播放

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class VideoTest : MonoBehaviour
{
    public VideoPlayer vp;

    public RawImage  image;
    public Slider progression;
    public Text timeCount;
    public Text currentTime;
    void Start()
    {
        vp.Play();
        progression.value = vp.GetVideoProgression();
        progression.onValueChanged.AddListener(Changed);
        DateFormat((int)vp.GetVideoTimeCount(), timeCount);
    }
 

//格式化视频播放时间的显示
    private void DateFormat(int sec, Text text)
    {
        TimeSpan span = new TimeSpan(0, 0, 0, sec);
        text.text = (int)span.Hours + ":" + (int)span.Minutes + ":" + (int)span.Seconds;
    }
 
    // Update is called once per frame
    void Update()
    {

      //如果videoPlayer没有对应的视频texture,则返回
        if (videoPlayer.texture == null)
        {
            return;
        }
        //把VideoPlayerd的视频渲染到UGUI的RawImage
        image.texture = videoPlayer.texture;
        DateFormat((int)vp.time, currentTime);
    }
    private void Changed(float value)
    {
        vp.SetVideoProgression(value);
    }
    public void Play()
    {
        vp.Play();
    }
    public void Pause()
    {
        vp.Pause();
    }
}
总体来说这个控制视频播放的组件还是蛮方便的

 

你可能感兴趣的:(videoPlayer)