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

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.kler.cn/a/147693.html

相关文章:

  • Linux运维常见命令
  • 详细讲解axios封装与api接口封装管理
  • 解锁高效密码:适当休息,让学习状态满格
  • springboot使用自定义的线程池 完成 多线程执行网络请求,返回数据后,统一返回给前段
  • 视频监控平台:Liveweb视频汇聚融合平台智慧安防视频监控应用方案
  • 大数据-256 离线数仓 - Atlas 数据仓库元数据管理 正式安装 启动服务访问 Hive血缘关系导入
  • 一、TIDB基础
  • 部署Jenkins
  • Spring框架体系及Spring IOC思想
  • Ruoyi报‘com.ruoyi.system.api.RemoteLogService‘ that could not be found.
  • 典型的SAST支持检测标准
  • C语言:写一个函数,实现3*3矩阵的转置(指针)
  • 美团小游戏rpk包对接流程小记
  • 戴上HUAWEI FreeBuds Pro 3, 近离城市喧嚣,尽情享受无损音质在耳边漫游
  • 【Python】itertools模块,补充:可迭代对象、迭代器
  • Linux CentOS7 LVM
  • python通过numpy进行排序
  • 微服务--02--Nacos注册中心
  • Android RGB转YUV的算法
  • 网站定制开发主要分类有哪些|企业 app 软件小程序定制
  • ELK+kafka+filebeat企业内部日志分析系统
  • ChatGPT等模型:到2026年,将消耗尽高质量训练数据
  • K8S----taint、tolerations、label
  • 正则化与正则剪枝
  • ②⑩① 【MySQL】什么是分库分表?拆分策略有什么?什么是MyCat?
  • 22款奔驰GLE450升级原厂360全景影像 超广角的视野