我一直在使用本地和网络播放支持的图像/视频播放器。
对于本地文件,我从“C:\ medias”这样的路径获取媒体,而对于网络,我使用的网址如“www.rtcmagazine.com/files/images/5353 /”
这似乎适用于图像,但是当涉及到视频时,我有点迷失。
我将RawImage用于图像和视频。我希望能够使用.mp4和.avi等主要视频格式,但RawImage只能使用.ogg / .ogv。这些文件似乎口吃,也有低fps。音频工作正常。
我应该如何处理这个问题。是否有插件或其他东西允许使用具有更好帧速率的主要视频格式,是否可以使用相同的RawImage UI对象?
我不需要任何播放/暂停按钮。对于图像,这应该是静态展示时间的无限循环,但视频应该运行它的长度。
欢迎任何帮助:)
..我的C#和Unity也有点生锈。
到目前为止我的脚本 - >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
public class Player : MonoBehaviour
{
// Use this for initialization
bool develop = true;
bool web = true;
//UI & Audio
public MovieTexture movie;
private AudioSource audio;
//Local
private string path = "file://";
private string folder = "C:/medias/";
int interval = 7;
int arrLength;
int i = 0;
//Web (http)
private string webpath = "http://";
string uri = "www.rtcmagazine.com/files/images/5353/";
string source;
string str;
WWW www;
//Extensions to get
string[] extensions = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".ogv", ".OGV" };
FileInfo[] info;
DirectoryInfo dir;
void Start()
{
if (web) {
string[] fetchFilesCount = GetFiles (webpath+uri);
int getLenght = fetchFilesCount.Length;
info = new FileInfo[getLenght];
int counter = 0;
string[] filesFromWeb = GetFiles (webpath+uri);
foreach (String file in filesFromWeb)
{
info [counter] = new FileInfo(webpath+uri+file);
counter++;
if (counter == filesFromWeb.Length) {
counter = 0;
}
}
} else {
info = new FileInfo[]{};
dir = new DirectoryInfo(@folder);
info = dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
}
arrLength = info.Length;
StartCoroutine(looper());
}
IEnumerator looper()
{
WaitForSeconds waitTime = new WaitForSeconds(interval);
//Run forever
while (true)
{
if (i == arrLength - 1)
{
if (web) {
int counter = 0;
string[] filesFromWeb = GetFiles (webpath+uri);
foreach (String file in filesFromWeb)
{
info [counter] = new FileInfo(webpath+uri+file);
counter++;
if (counter == filesFromWeb.Length) {
counter = 0;
}
}
} else {
info = dir.GetFiles ().Where (f => extensions.Contains (f.Extension.ToLower ())).ToArray ();
arrLength = info.Length;
}
i = 0;
}
else
{
i++;
}
yield return StartCoroutine(medialogic());
//Wait for 7 seconds
yield return waitTime;
}
}
IEnumerator medialogic()
{
source = info[i].ToString();
if (web) {
if (develop) {
www = new WWW (source);
} else {
www = new WWW (webpath+uri+source);
}
} else {
if (develop) {
str = path + source;
} else {
str = path + folder + source;
}
www = new WWW (str);
}
//Wait for download to finish
yield return www;
int index = source.LastIndexOf('.');
//string lhs = index < 0 ? source : source.Substring(0,index),
string rhs = index < 0 ? "" : source.Substring(index+1);
int pos = Array.IndexOf(extensions, "." + rhs);
if (pos > -1 && pos < 6)
{
GetComponent<RawImage>().texture = www.texture;
}
else if (pos > 5)
{
//videos here
MovieTexture movieToPlay = www.movie as MovieTexture;
GetComponent<RawImage> ().texture = movieToPlay;
audio = GetComponent<AudioSource> ();
audio.clip = movieToPlay.audioClip;
movieToPlay.Play ();
audio.Play ();
}
}
public static string[] GetFiles(string url)
{
string[] extensions2 = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".ogv", ".OGV" };
List<string> files = new List<string>(500);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Regex regex = new Regex("<a href=\".*\">(?<name>.*)</a>");
MatchCollection matches = regex.Matches(html);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
if (match.Success)
{
string[] matchData = match.Groups[0].ToString().Split('\"');
foreach (string x in extensions2)
{
if (match.ToString().Contains(x))
{
files.Add(matchData[1]);
}
}
//files.Add(matchData[1]);
}
}
}
}
}
return files.ToArray();
}
public static string[] getFtpFolderItems(string ftpURL)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);
request.Method = WebRequestMethods.Ftp.ListDirectory;
//You could add Credentials, if needed
//request.Credentials = new NetworkCredential("anonymous", "password");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}
}