其他分享
首页 > 其他分享> > 【Unity】VideoPlayer根据视频命名规则读取并播放视频

【Unity】VideoPlayer根据视频命名规则读取并播放视频

作者:互联网

发代码之前补一下挖到的坑。

VideoPlayer 通过URL读取视频的时候,路径必须准确!如发现读取后Unity崩溃,基本就是读取的路径有问题。

 

该代码之所以扔出来.. 看图

(稍作修改...修改后没再次运行测试)

代码就大概这样...找streamingAssets下的movies文件夹中的视频,  但只抓 具备命名规则的  如 A.mp4 B.mp4等

(可自行修改规则- 规则适宜ASCII码连续的字符 或直接拿数字)。

根据index标识直接抓取,每次只抓对应标号的视频名。

抓到后构成路径,通过URL在VideoPlayer中读取视频。

然后根据计时进行视频切换。

 

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

public class VideoAutoPlayByName : MonoBehaviour
{
    private VideoPlayer videoPlayer;
    string folderName = "/Movie";                                                                   //文件夹名字
    private string path = "";
    public string[] likePaths;
    private int beginIndexABC = (int)System.Text.ASCIIEncoding.ASCII.GetBytes("A")[0];              //ABC播放规则的索引值
    public int endIndexABC = (int)System.Text.ASCIIEncoding.ASCII.GetBytes("Z")[0];
    private int currentIndex = 0;
    private float elapsedTime = 0;                  //消逝时间
    public float durationTime = 30;                 //持续多少秒后更换视频
    public bool isAuto = false;                                                                     //是否开启自动更换视频
    public bool defaultLooping = false;                 //是否默认循环播放

    private void Start()
    {
        FindVideoPlayer();
        InitPathString();
    }

    private void Update()
    {
        UpdateIndex();
    }

    private void InitPathString()
    {
        path = Application.streamingAssetsPath + folderName + "s/";
        currentIndex = (int)System.Text.ASCIIEncoding.ASCII.GetBytes("A")[0];
        likePaths = new string[endIndexABC - beginIndexABC + 1];
        for (int i = 0; i < likePaths.Length; i++)
        {
            likePaths[i] = "* " + (char)(beginIndexABC + i) + ".mp4";
        }
    }


    public void ReadMoviesByName()
    {
        //检索目录下的文件
        DirectoryInfo directoryInfo = new DirectoryInfo(path);
        FileInfo[] fileInfos = directoryInfo.GetFiles(likePaths[currentIndex - beginIndexABC]);

        if (fileInfos.Length > 0)
            SetVideoUrl(fileInfos[0].Name);
        else
        {
            currentIndex++;
            if (currentIndex > endIndexABC)
            {
                currentIndex = beginIndexABC;
            }
            ReadMoviesByName();
        }

    }

    private void UpdateIndex()
    {
        if (isAuto && endIndexABC - beginIndexABC > 1)
        {
            elapsedTime += Time.deltaTime;
            if (elapsedTime >= durationTime)
            {
                currentIndex++;
                if (currentIndex > endIndexABC)
                {
                    currentIndex = beginIndexABC;
                }
                ReadMoviesByName();
                elapsedTime = 0;
            }
        }
    }


    //读取视频文件
    private void SetVideoUrl(string videoName)
    {
        if (File.Exists(path + videoName))
        {
            videoPlayer.Stop();

            videoPlayer.url = path + videoName;
            videoPlayer.Play();
        }
        else
        {
            Debug.Log("文件不存在!!");
        }
    }

    private void FindVideoPlayer()
    {
        videoPlayer = FindObjectOfType<VideoPlayer>();
        if (videoPlayer == null)
        {
            Debug.LogError("缺少名为'Plane'背景板物体");
            //自行创建  不创建就直接return了
            return;
        }
        videoPlayer.isLooping = defaultLooping;

    }
}

 

标签:视频,void,beginIndexABC,VideoPlayer,private,videoPlayer,Unity,currentIndex,public
来源: https://blog.csdn.net/Terrell21/article/details/100779154