Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class VideoLoadManager : Singleton<VideoLoadManager>
{
public enum TourType
{
Guided,
Manual,
None
}
public AudioSource spatialSound;
public TourType tourType;
private MediaPlayerCtrl mediaPlayerCtrl;
private string path;
private int videos_count;
private int audio_count;
private int current_video;
private bool videoEnded = false;
private string[] VideoList;
private void Start()
{
tourType = TourType.None;
current_video = 1;
mediaPlayerCtrl = GetComponent<MediaPlayerCtrl>();
if (mediaPlayerCtrl == null)
{
mediaPlayerCtrl = GetComponent<MediaPlayerCtrl>();
if (mediaPlayerCtrl == null)
MessageHandler.Instance.SetMessageText("No Media Player Ctrl object in scene.");
}
path = Application.persistentDataPath;
videos_count = CountVideos();
if (videos_count < 10)
{
MessageHandler.Instance.SetMessageText("Not enough videos in the destination folder.");
}
audio_count = CountAudio();
if (audio_count == 0)
{
MessageHandler.Instance.SetMessageText("Not enough audio files in the destination folder.");
}
VideoList = GetVideoList();
}
public void PauseVideo()
{
if (mediaPlayerCtrl)
{
mediaPlayerCtrl.Pause();
}
if (spatialSound)
{
spatialSound.Pause();
}
}
public void ResumeVideo()
{
if (!videoEnded)
{
if (mediaPlayerCtrl)
{
mediaPlayerCtrl.Play();
}
if (spatialSound)
{
spatialSound.Play();
}
}
}
public void SetManualTour()
{
tourType = TourType.Manual;
}
public void SetGuidedTour()
{
tourType = TourType.Guided;
current_video = 1;
}
public void LoadSelectedVideo(string video_index)
{
StopPlayback();
current_video = Convert.ToInt32(video_index);
//mediaPlayerCtrl.Load("file://" + path + "/videos/video" + video_index + ".mp4");
mediaPlayerCtrl.Load("file://"+ VideoList[current_video]);
Debug.Log("file://"+ VideoList[current_video]);
mediaPlayerCtrl.OnReady += OnVideoReady;
mediaPlayerCtrl.Play();
videoEnded = false;
if (tourType == TourType.Manual)
{
mediaPlayerCtrl.OnEnd += ManualVideoEnd;
}
else
{
mediaPlayerCtrl.OnEnd += GuidedVideoEnd;
}
}
public void LoadIntroVideo()
{
string video_index = "0";
StopPlayback();
current_video = Convert.ToInt32(video_index);
mediaPlayerCtrl.Load("file://" + VideoList[current_video]);
Debug.Log("file://" + VideoList[current_video]);
mediaPlayerCtrl.OnReady += OnVideoReady;
mediaPlayerCtrl.Play();
videoEnded = false;
mediaPlayerCtrl.OnEnd += IntroVideoEnd;
}
public string[] GetVideoList()
{
string[] dirs;
try
{
// Only get files that begin with the letter "c."
dirs = Directory.GetFiles(path + "/videos/", "*.mp4");
Console.WriteLine("The number of files ending with *.mp4 is {0}.", dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
//Debug.Log(dir);
}
return dirs;
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
return new string[] { };
}
}
private void OnVideoReady()
{
LoadAudioFromStorage(current_video.ToString());
InputManagerScript.Instance.isDisabled = false;
MenuManager.Instance.VideoReadyFadeIn(current_video.ToString());
mediaPlayerCtrl.OnReady -= OnVideoReady;
}
public void ManualVideoEnd()
{
InputManagerScript.Instance.isDisabled = true;
mediaPlayerCtrl.OnEnd -= ManualVideoEnd;
HotButtonsManager.Instance.SetVisited(current_video);
videoEnded = true;
if (current_video >= videos_count)
{
current_video = 1;
MenuManager.Instance.ShowThankYouMenu();
}
else
{
MenuManager.Instance.ShowMidsceneMenu();
}
}
public void GuidedVideoEnd()
{
InputManagerScript.Instance.isDisabled = true;
mediaPlayerCtrl.OnEnd -= GuidedVideoEnd;
HotButtonsManager.Instance.SetVisited(current_video);
videoEnded = true;
LoadNextVid();
}
public void IntroVideoEnd()
{
InputManagerScript.Instance.isDisabled = true;
mediaPlayerCtrl.OnEnd -= IntroVideoEnd;
videoEnded = true;
}
public void LoadAudioFromStorage(string sound_index)
{
spatialSound.Stop();
StartCoroutine(LoadAudioCoroutine(sound_index));
}
IEnumerator LoadAudioCoroutine(string sound_index)
{
string path = "file://" + Application.persistentDataPath + "/audio/audio" + sound_index + ".wav";
WWW www = new WWW(path);
yield return www;
spatialSound.clip = www.GetAudioClip(false, false, AudioType.WAV);
Debug.Log("Load Audio: " + path);
spatialSound.Play();
}
private int CountVideos()
{
int x = 0;
foreach (string file in System.IO.Directory.GetFiles(path + "/videos"))
{
x++;
}
return x;
}
private int CountAudio()
{
int x = 0;
foreach (string file in System.IO.Directory.GetFiles(path + "/audio"))
{
x++;
}
return x;
}
public void LoadNextVid()
{
if (!InputManagerScript.Instance.isHeadsetOff)
{
StopPlayback();
current_video++;
if (current_video > videos_count)
{
current_video = 1;
MenuManager.Instance.ShowThankYouMenu();
}
else
{
MenuManager.Instance.VideoLoad(current_video.ToString());
}
}
}
public bool IsTourVideoPlaying()
{
if (mediaPlayerCtrl.GetCurrentState() == MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING)
{
return true;
}
return false;
}
public void StopPlayback()
{
if (mediaPlayerCtrl)
{
mediaPlayerCtrl.OnEnd -= GuidedVideoEnd;
mediaPlayerCtrl.OnEnd -= ManualVideoEnd;
mediaPlayerCtrl.Stop();
}
if (spatialSound)
{
spatialSound.Stop();
}
}
public int GetCurrentVideo()
{
return current_video;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.