1

I have been working on this image/video player with local and web playback support.

For local files I get the medias from a path like "C:\medias" and for web I use url like "www.rtcmagazine.com/files/images/5353/"

This seems to work well with images, but when it comes to videos I'm a bit lost.

I use RawImage for images and videos. I want to be able to use main video formats like .mp4 and .avi, but RawImage only takes .ogg/.ogv. These files seems to stutter and have low fps also. Audio works fine.

How should I process this. Is there a plugin or something to allow use of main video formats with better frame rate and is it usable with the same RawImage UI object?

I dont need any play/pause buttons. This should be infinite loop with static showtime for images, but videos should run for its length.

Any help is welcome :)

..also my C# and Unity is a bit rusty.

My script so far ->

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);
    }
}
Jack M.
  • 1,831
  • 5
  • 24
  • 36

0 Answers0