当前位置: 首页 > news >正文

Unity播放网络视频

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

/// <summary> 视频UI面板 </summary>
public class VideoUIForm : BaseUIForm
{
    private ImageAdaptive imageAdaptive;
    private RawImage rawImage;
    private VideoPlayer videoPlayer;
    private bool isPlaying = false;
    private Slider slider;
    private Text playTimeText;
    private Text lengthText;
    private GameObject playButtonGo;
    private GameObject playIconGo;
    private GameObject pauseIconGo;
    private bool sliderIsDrag = false;

    private float playTime;
    private float length;

    public override void OnAwake()
    {
        base.OnAwake();

        init();

        rigisterButtonEvent();
    }

    private void init()
    {
        slider = UnityHelper.FindTheChildNode(gameObject, "Slider").GetComponent<Slider>();
        rawImage = UnityHelper.FindTheChildNode(gameObject, "Stage").GetComponent<RawImage>();
        rawImage.enabled = false;
        videoPlayer = rawImage.gameObject.GetComponent<VideoPlayer>();
        if (videoPlayer == null) videoPlayer = rawImage.gameObject.AddComponent<VideoPlayer>();
        playTimeText = UnityHelper.FindTheChildNode(gameObject, "PlayTime").GetComponent<Text>();
        lengthText = UnityHelper.FindTheChildNode(gameObject, "Length").GetComponent<Text>();
        playButtonGo = UnityHelper.FindTheChildNode(gameObject, "BtnPlay").gameObject;
        playIconGo = UnityHelper.FindTheChildNode(gameObject, "PlayIcon").gameObject;
        pauseIconGo = UnityHelper.FindTheChildNode(gameObject, "PauseIcon").gameObject;

        imageAdaptive = rawImage.GetComponent<ImageAdaptive>();
        if (imageAdaptive == null) imageAdaptive = rawImage.gameObject.AddComponent<ImageAdaptive>();

        videoPlayer.playOnAwake = false; // 设置不自动播放
        videoPlayer.waitForFirstFrame = true; // 等待第一帧加载完成再显示
        videoPlayer.source = VideoSource.Url;
        videoPlayer.aspectRatio = VideoAspectRatio.FitVertically;

        videoPlayer.prepareCompleted += onVideoPrepared; // 视频加载完成事件
        videoPlayer.loopPointReached += onVideoEnd; // 视频播放完成事件

        playIconGo.SetActive(true);
        pauseIconGo.SetActive(false);

        initFinish = true;
    }

    void Update()
    {
        if (!sliderIsDrag)
        {
            float progress = (float)videoPlayer.time / (float)videoPlayer.length;
            if (progress > 0) slider.value = progress;
            length = (float)videoPlayer.length;
            lengthText.text = formatTime(length);
        }

        playTime = (float)videoPlayer.time;
        playTimeText.text = formatTime(playTime);
        playButtonGo.SetActive(!isPlaying);
        playIconGo.SetActive(!isPlaying);
        pauseIconGo.SetActive(isPlaying);
    }

    /// <summary>关闭UI窗口事件</summary>
    public override void OnCloseUIEvent()
    {
        base.OnCloseUIEvent();

        clearData();
    }

    public override void OnUIMsgEvent(string key, object values)
    {
        base.OnUIMsgEvent(key, values);

        switch (key)
        {
            case MsgDefine.UPDATE_PANEL_DATA:
                if (values != null)
                {
                    if (initFinish)
                    {
                        rawImage.enabled = false;

                        string path = (string)values;
                        string encodedUrl = System.Uri.EscapeUriString(path);
                        videoPlayer.url = encodedUrl;
                        videoPlayer.Play();
                        isPlaying = true;

                    }
                }
                break;
        }

    }

    /// <summary>注册按钮事件</summary>
    private void rigisterButtonEvent()
    {
        RigisterButtonEvent("BtnClose", (clickObj) => { CloseCurrentUIForm(); });
        RigisterButtonEvent("BtnPlayAndPause", (clickObj) => { PlayPauseVideo(); });
        RigisterButtonEvent("BtnPlay", (clickObj) =>
        {
            videoPlayer.Play();
            isPlaying = true;
        });
    }

    /// <summary>进度条拖动发送改变</summary>
    public void OnSliderDragChange(bool isDrag)
    {
        sliderIsDrag = isDrag;
    }

    /// <summary>清理数据</summary>
    private void clearData()
    {
        if (!initFinish) return;

        isPlaying = false;
        slider.value = 0;
        playTimeText.text = "00:00";
        lengthText.text = "00:00";
        playButtonGo.SetActive(true);
        playIconGo.SetActive(true);
        pauseIconGo.SetActive(false);
        sliderIsDrag = false;
    }

    /// <summary>视频准备完成</summary>
    private void onVideoPrepared(VideoPlayer source)
    {
        rawImage.texture = source.texture;
        source.Play();
        rawImage.enabled = true;
        isPlaying = true;

        // 获取视频的宽度和高度
        uint width = source.width;
        uint height = source.height;

        RectTransform parentRect = transform.parent.GetComponent<RectTransform>();
        if (parentRect != null)
        {
            imageAdaptive.BoundarySize = new Vector2(parentRect.rect.width, parentRect.rect.height);
            imageAdaptive.ImageSize = new Vector2(width, height);
        }

    }

    /// <summary>视频播放结束</summary>
    private void onVideoEnd(VideoPlayer source)
    {
        source.Pause();
        isPlaying = false;
    }

    /// <summary>播放或者暂停视频</summary>
    public void PlayPauseVideo()
    {
        if (isPlaying)
        {
            videoPlayer.Pause();
            isPlaying = false;
        }
        else
        {
            videoPlayer.Play();
            isPlaying = true;
        }
    }

    /// <summary>设置视频播放进度</summary>
    public void setVideoProgress(float progress)
    {
        if (videoPlayer.canSetTime)
        {
            videoPlayer.time = videoPlayer.clip.length * progress;
        }
    }

    /// <summary>格式化时间</summary>
    private string formatTime(float time)
    {
        int minutes = Mathf.FloorToInt(time / 60f);
        int seconds = Mathf.FloorToInt(time % 60f);

        return string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

http://www.lryc.cn/news/246431.html

相关文章:

  • SCI一区级 | Matlab实现GWO-CNN-LSTM-selfAttention多变量多步时间序列预测
  • 线性分类器--图像表示
  • 车载通信架构 —— 传统车内通信网络FlexRay(较高速度高容错、较灵活拓扑结构)
  • 如何在Ubuntu的Linux系统中安装MySQL5.7数据库
  • 基于Hadoop的区块链海量数据存储的设计与实现
  • 运行时错误/缺陷到底是什么缺陷
  • 应用Web3.0的5种方法提升你的点击量
  • 计算机服务器中了mallox勒索病毒如何处理,mallox勒索病毒解密文件恢复
  • 408—电子笔记分享
  • 【每日一题】子数组的最小值之和
  • 【docker】docker总结
  • [英语学习][3][Word Power Made Easy]的精读与翻译优化
  • 使用UIActivityViewController分享图片,没有preview
  • linux安装终端连接工具Tabby
  • Linux telnet命令详解:通过TCP/IP网络连接与管理远程机器(附实例教程和注意事项)
  • linux 磁盘管理、分区管理常用命令
  • Milvus入门手册1.0
  • PCL 计算两点云之间的最小距离
  • 基于YOLOv5的视频计数 — 汽车计数实现
  • jetson nano 串口通信
  • Vue基础入门(三):Vue3的使用
  • 基于M估计样本一致性算法的点云平面拟合
  • 【VRTK】【VR开发】【Unity】8-可交互对象
  • Huggingface 超详细介绍
  • pycharm 怎么切换Anaconda简单粗暴
  • 笔记二十二、使用路由state进行传递参数
  • 2023 OI 总结
  • ESP32-Web-Server编程-HTML 基础
  • 【docker】docker安装与优化
  • https到底把什么加密了?