Unity Vedioplayer应用

暂停播放+进度条控制

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

public class VedioPlayPanel : MonoBehaviour
{
    public VideoPlayer videoPlayer;

    public Slider _videoSeekSlider;

    public Image vedioPauseImg;

    public Toggle vedioToggle;

    public Text titleText, nowTime, vedioTotalTime;

    private bool isPausing, isDrag = false;

    private void Start()
    {
        vedioToggle.onValueChanged.AddListener(PlayAndPause);

        _videoSeekSlider.onValueChanged.AddListener((value) => { ChangeVideoFrame(); });

        videoPlayer.loopPointReached += (VideoPlayer source) =>
        {
            videoPlayer.frame = 0;

            vedioToggle.isOn = false;
        };
    }

    private void FixedUpdate()
    {
        nowTime.text = GetFormatTimeText(videoPlayer.time);

        if (!isDrag)
        {
            _videoSeekSlider.value = (float.Parse(videoPlayer.frame.ToString()) / float.Parse(videoPlayer.frameCount.ToString()));
        }
    }

    public void PlayVedioOnInit(string vedioPath)
    {
        videoPlayer.targetTexture.Release();

        titleText.text = vedioPath.Split("/")[1].Substring(0, 2);

        videoPlayer.url = Application.streamingAssetsPath + "/" + vedioPath;

        videoPlayer.Play();

        videoPlayer.audioOutputMode = VideoAudioOutputMode.Direct;

        Invoke("InitTotalText", 0.5f);

        switch (titleText.text)
        {
            case "规划":
                StartCoroutine(NetWorkInterfaceManager.SendUserCtrMsg(1));
                break;
            case "建设":
                StartCoroutine(NetWorkInterfaceManager.SendUserCtrMsg(2));
                break;
            case "运维":
                StartCoroutine(NetWorkInterfaceManager.SendUserCtrMsg(3));
                break;
            default:
                break;
        }
    }

    private void InitTotalText()
    {
        vedioTotalTime.text = GetFormatTimeText(videoPlayer.length);
    }

    public void PlayAndPause(bool ison)
    {
        isPausing = !ison;

        vedioPauseImg.gameObject.SetActive(!ison);

        if (ison)
        {
            videoPlayer.Play();
        }
        else
        {
            videoPlayer.Pause();
        }
    }

    public void ChangeVideoFrame()
    {
        if (isDrag)
        {
            videoPlayer.frame = long.Parse((_videoSeekSlider.value * videoPlayer.frameCount).ToString("0."));
        }
    }

    public void OnSliderDown()
    {
        isDrag = true;

        videoPlayer.Pause();

        ChangeVideoFrame();
    }

    public void OnSliderUp()
    {
        isDrag = false;

        if (!isPausing)
        {
            videoPlayer.Play();
        }
    }

    public void ClosePanel()
    {
        videoPlayer.Stop();

        gameObject.SetActive(false);
    }

    private string GetFormatTimeText(double second)
    {
        return string.Format("{0:D2}:{1:D2}", (int)second / 60, (int)second % 60);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值