• 可循环视频播放器丨VideoPlayer丨StreamingAssets加载


    可循环视频播放器丨VideoPlayer丨StreamingAssets加载


    提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档

    问题介绍

    本文制作为Unity的视频播放器功能,解决播放器循环不了的问题,呈现问题为多个视频列表最后一个视频无法跳转下一个视频播放的问题


    一、创建

    一、首先我们要创建一个RawImage和三个Button按钮和一个Text (text可有可无吧) 和一个脚本
    在这里插入图片描述
    在这里插入图片描述
    二、在我们RawImage上挂载我们脚本和Videoplayer组件
    source选项我们修改为URL
    在这里插入图片描述

    三、确保我们文件夹的存在和视频的存在
    此处我们当前脚本为StreamingAssets中创建了一个Video的文件夹名称
    上边的Videopath可以决定文件夹名称
    在这里插入图片描述

    二、代码部分

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Video;
    
    public class Video_Controller : MonoBehaviour
    {
        private VideoPlayer videoplayer;
        private RawImage rawImage;
        private int currentClipIndex;
    
        public Text text_playorpause;
        public Button button_playorpause;
        public Button button_pre;
        public Button button_next;
        private List<string> videoClips;//定义了一个数组
        string videopath = "Video1";
    
        // Use this for initialization
        void Start()
        {
            videoplayer = this.GetComponent<VideoPlayer>();//获取组件
            rawImage = this.GetComponent<RawImage>();
            currentClipIndex = 0;
            button_playorpause.onClick.AddListener(OnplayorpauseVideo);
            button_pre.onClick.AddListener(OnpreVideo);
            button_next.onClick.AddListener(OnnextVideo);//调用方法
            Fun();
        }
    
        public void Fun()
        {
            var path = Application.streamingAssetsPath + "/" + videopath;
            if (Directory.Exists(path))
            {
                DirectoryInfo direction = new DirectoryInfo(path);
                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
                videoClips = new List<string>();
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].Name.EndsWith(".meta"))
                    {
                        continue;
                    }
                    videoClips.Add(files[i].Name);
                    foreach (string item in videoClips)
                    {
                        if (item != null)
                        {
                            Debug.Log(item);
    
    
                        }
    
                    }
                    // Debug.Log("Name : " + files[i].Name);
                    // Debug.Log("FullName : " + files[i].FullName);
                    // Debug.Log("DirectoryName : " + files[i].DirectoryName);
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (videoplayer.texture == null)
            {
                return;
            }
            rawImage.texture = videoplayer.texture;
    
    
    
        }
       
        private void OnplayorpauseVideo()
        {
            if (videoplayer.enabled == true)
            {
                if (videoplayer.isPlaying)
                {
                    videoplayer.Pause();
                    text_playorpause.text = "播放";
                    Debug.Log("2322");//用于调试脚本不能正常运行
    
                }
                else if (!videoplayer.isPlaying)
                {
                    videoplayer.Play();
                    Debug.Log("111");
                    text_playorpause.text = "暂停";
                }
            }
        }
        private void OnpreVideo()
        {
            currentClipIndex -= 1;
            if (currentClipIndex < 0)
            {
                currentClipIndex = videoClips.Count - 1;
            }
            videoplayer.url = "file://" + Application.streamingAssetsPath + "/" + videopath + "/" + videoClips[currentClipIndex];
            text_playorpause.text = "暂停";
        }
        private void OnnextVideo()
        {
            currentClipIndex += 1;
            currentClipIndex = currentClipIndex % videoClips.Count;
            videoplayer.url = "file://" + Application.streamingAssetsPath + "/" + videopath + "/" + videoClips[currentClipIndex];
            text_playorpause.text = "暂停";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    总结

    例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 相关阅读:
    Mysql主从复制
    ClickHouse备份方案
    背会了常见的几个线程池用法,结果被问翻了
    面试:听说你用过 MQ,能具体说说吗?
    Opencv-图像插值与LUT查找表
    【愚公系列】2022年11月 微信小程序-全局数据共享
    47.各种类型的线程池
    WPA-Supplicant 极简交叉编译
    二叉树实现快速查找数字所在区间
    [附源码]计算机毕业设计JAVA学生宿舍设备报修
  • 原文地址:https://blog.csdn.net/weixin_42746271/article/details/127793817