可循环视频播放器丨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提供了大量能使我们快速便捷地处理数据的函数和方法。