组件:AI 3d数字人展示场地视频加载组件

效果:
在这里插入图片描述
在这里插入图片描述

代码:

using com.diyabc.aliyun;
using com.diyabc.data;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityEngine.Video;

public class VideoLoader : MonoBehaviour
{
public string videoURL = “####/system/videos/bgs/v/IMG_6199.MOV”;
public VideoPlayer videoPlayer;
public MeshRenderer modelRenderer;
public Material Material;
public int materialIndex = 1;

void Start()
{

    StartCoroutine(LoadVideo());
}

public static ConcurrentDictionary<string, VideoPlayer> vedioList = new ConcurrentDictionary<string, VideoPlayer>();
IEnumerator LoadVideo()
{
    if(string.IsNullOrEmpty(DirManager.basedir))
    {
        DirManager.basedir=Application.persistentDataPath;//
    }
    modelRenderer =this.GetComponent<MeshRenderer>();
    if(modelRenderer.materials != null)
    {
        if(modelRenderer.materials.Length >= materialIndex)
        {
            Material = modelRenderer.materials[materialIndex];
        }
        else
        {
            Material=modelRenderer.material;
        }
    }
    //
    videoURL = "https://diyabc.oss-cn-hangzhou.aliyuncs.com/system/videos/bgs/h/IMG_7885.MOV";//横屏
    //videoURL = "https://diyabc.oss-cn-hangzhou.aliyuncs.com/system/videos/bgs/v/IMG_6199.MOV"; 

    if (vedioList.ContainsKey(videoURL))//如果已经有WideoPayer 加载了這个视频,那么就直接取纹理好了,要判断是否在播放状态
    {
        this.StartCoroutine(ReTagetTexture(videoURL));
        yield break;
    }

    string objectKey = AliyunOSS.GetObjectName(videoURL);

    string fileLocal = DirManager.GetFile(objectKey);// "";
    Debug.LogError($"{objectKey} fileLocal = {fileLocal} ");
    UnityWebRequest request = UnityWebRequest.Get(videoURL);

    videoPlayer = this.GetComponent<VideoPlayer>();
    if (videoPlayer == null)
    {
        videoPlayer = gameObject.AddComponent<VideoPlayer>();  // 添加 VideoPlayer 组件
    }
    videoPlayer.playOnAwake = false;
    videoPlayer.waitForFirstFrame = true;
    videoPlayer.isLooping = true;
    vedioList.TryAdd(videoURL, videoPlayer);

    

    videoPlayer.renderMode = VideoRenderMode.RenderTexture;// VideoRenderMode.APIOnly;
    videoPlayer.playOnAwake = false;  // 设置不自动播放
    videoPlayer.source = VideoSource.Url;  // 设置视频来源为 URL
                                           // videoPlayer.url = Application.dataPath + "/a.mp4";  // 设置视频文件路径
    bool isLoacl = false;
    // 检查本地是否存在缓存文件
    if (System.IO.File.Exists(fileLocal))
    {
        isLoacl = true;
        //request.SetRequestHeader("Range", "bytes=" + new System.IO.FileInfo(fileLocal).Length + "-");
    }
    else
    {
        yield return request.SendWebRequest();//开始下载
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            //下载成功 // 缓存视频文件到本地
            System.IO.File.WriteAllBytes(fileLocal, request.downloadHandler.data);
        }
    }
    // 设置视频播放器的 URL
    videoPlayer.url = fileLocal;
    //Debug.LogError($"{fileLocal}");
    videoPlayer.Prepare();  // 预加载视频
    // 监听视频加载完成事件
    videoPlayer.prepareCompleted += (VideoPlayer vp) =>
    {
        //rawImage.texture = vp.texture;  // 将第一帧图像显示在 RawImage 上
        var txt = vp.texture;
        vp.Play();  // 播放视频
                    //SaveFirstFrame(vp.texture);  // 将第一帧图像保存到本地文件
                    // 设置材质纹理为视频帧
        if (videoPlayer.targetTexture != null)
            videoPlayer.targetTexture.Release();
        Debug.LogError($"txt.width {txt.width }, txt.height { txt.height} ");
        videoPlayer.targetTexture = new RenderTexture(txt.width, txt.height, 16, RenderTextureFormat.ARGB32);
        if (Material != null)
        {
            Material.mainTexture=videoPlayer.targetTexture;
        }
    };

}
public int cc = 0;
//public static ConcurrentDictionary<VideoLoader, int> objInstanceID = new();
private IEnumerator ReTagetTexture(string videoURL)
{ 
    yield return new WaitForSeconds(1f);
    if(vedioList.TryGetValue(videoURL, out var vedio))
    {
        while (vedio.targetTexture==null&& this.cc<300)//5分钟
        {
            this.cc=this.cc+1;
            yield return new WaitForSeconds(1f);
        }

        if (Material != null&& vedio.targetTexture!=null)
        {
            Material.mainTexture = vedio.targetTexture;
        }
    }
   
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值