关于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);
    }
    /// <summary>
    /// 获取视频进度
    /// </summary>
    /// <param name="vsp"></param>
    /// <returns></returns>
    public static float GetVideoProgression(this VideoPlayer vp)
    {
        return (float)((vp.time * vp.frameRate)/(vp.frameCount / vp.frameRate));
    }
 
    /// <summary>
    /// 设置视频进度
    /// </summary>
    /// <param name="vp"></param>
    /// <param name="progression"></param>
    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();
    }
}
总体来说这个控制视频播放的组件还是蛮方便的

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值