unity 实现一个视频播放暂停 slider滑动进度 的播放视频模块

34 篇文章 0 订阅

下图是最终效果
如图
下图是用来播放视频的UI 上面需要挂的俩个组件
预制设置
下图是创建VideoRenderTexture的步骤
需要创建的Video Render Texture
上代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Video;

public class VideoView : MonoBehaviour
{
    public Button closeBtn;
    public VideoPlayer video;
    public Toggle tog;//暂停与播放
    public MySlider slider;//作为滑动进度的
    public Text timeText;//显示文字的
    public Image Fill;//作为显示进度的

    // Start is called before the first frame update
    void Awake()
    {
        video.source = VideoSource.Url;
        tog.onValueChanged.AddListener((pag) =>
        {
            tog.transform.GetChild(0).gameObject.SetActive(!pag);
            if (pag)
            {
                video.Pause();
            }
            else
            {
                video.Prepare();
                video.Play();
                UnityHttpHelper.Ins.StartCoroutine(OnUpdate());
            }
        });
        slider.onValueChanged.AddListener((o) => { video.frame = long.Parse((o * video.frameCount).ToString("0.")); Fill.fillAmount = o; SetTime(); });
        slider.beiginDrag = beiginDrag;
        slider.endDrag = endDrag;
        closeBtn.onClick.AddListener(() => { gameObject.SetActive(false); });
    }
    bool taggle = true;
    private void endDrag()
    {
        taggle = true;
    }

    private void beiginDrag()
    {
        taggle = false;
    }

    public void SetVideo(string url)
    {
        video.Prepare();
        video.url = url;
        gameObject.SetActive(true);
        OnPlayerFalse(true);
        UnityHttpHelper.Ins.StartCoroutine(OnUpdate());
    }
    int hour = 0;//小时
    int minte = 0;//分钟
    int second = 0;//秒
    int hourAll = 0;
    int minteAll = 0;
    int secondAll = 0;

    IEnumerator OnUpdate()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.05f);
            //await Task.Delay(5);
            if (video.isPlaying == true)
            {
                break;
            }
        }
        while (true)
        {
            yield return new WaitForSeconds(0.05f);
            //await Task.Delay(5);
            if (video.url == null) yield break;
            //slider.value = (float.Parse(video.frame.ToString()) / float.Parse(video.frameCount.ToString()));
            if (taggle == true)
                Fill.fillAmount = (float.Parse(video.frame.ToString()) / float.Parse(video.frameCount.ToString()));
            SetTime();
            if (video.isPlaying == false)
            {
                Debug.Log("111111111111");
                OnPlayerFalse(video.isPlaying);
                yield break;
            }
        }
    }
    void SetTime()
    {
        //hour = (int)(video.time / 3600);
        //minte = (int)((video.time - hour * 3600) / 60);
        //second = (int)(video.time - hour * 3600 - minte * 60);   
        minte = (int)(video.time / 60);
        second = (int)(Math.Round(video.time) - (ulong)(minte * 60));
        //minte = (int)(video.frame / 60);
        //second = (int)(video.frame - (minte * 60));
        minteAll = (int)(video.length / 60);
        secondAll = (int)(video.length - minteAll * 60);
        timeText.text = $"{minte.ToString("D2")}:{second.ToString("D2")} / {minteAll.ToString("D2")}:{secondAll:D2}";
        //timeText.text = $"{hour.ToString("D2")}:{minte.ToString("D2")}:{second.ToString("D2")} / {hourAll.ToString("D2")}:{minteAll.ToString("D2")}:{secondAll:D2}";
    }
    //设置暂停与播放状态
    void OnPlayerFalse(bool isPlay)
    {
        tog.isOn = !isPlay;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值