当前位置:   article > 正文

unity 实现一个视频播放暂停 slider滑动进度 的播放视频模块_unityui上播放、暂停、停止视频,并使用slider控件拖动进度条

unityui上播放、暂停、停止视频,并使用slider控件拖动进度条

下图是最终效果
如图
下图是用来播放视频的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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/114090?site
推荐阅读
相关标签
  

闽ICP备14008679号