可循环视频播放器丨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 = "暂停";
    }

}


总结

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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Glunn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值