- Home /
How To Fetch movie Name From URL
Is there a way to display a movie name or title that is at the end of a url as a UI text on a canvas or panel so you can display a movie title? My script for how I am playing my movie is posted down below: Is it even possible to do this???
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class DynamicVideoX : MonoBehaviour {
public string _url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
public RawImage texture;
private MovieTexture movieTexture;
private WWW www;
public bool autoPlay = true;
private bool autoPlayed = false;
public bool loop = true;
//events
UnityEvent VideoStart = new UnityEvent ();
UnityEvent VideoEnd = new UnityEvent ();
//fades
private float alpha = 1f;
public bool fadeIn = true;
public bool fadeOut = true;
public float fadeInDuration = 2.0f;
public float fadeOutDuration = 2.0f;
private bool isFadingIn;
private bool isFadingOut;
private float alphaStart;
private float fadeCounter = 0.0f;
// Reference to my UI Display Text for Movie Now Playing
public Text NowPlayingText;
private string display = "";
//timer
public Text TimeCodeCurrent;
public Text TimeCodeDuration;
private float currentTime = 0;
//video complete verification
private bool isPlaying = false;
private int frameFreezedMax = 3;
private int frameFreezedCounter = 0;
private float lastCurrentTime = 0;
void Start () {
Url = _url;
}
void Update () {
//autoPlay
if (autoPlay && !autoPlayed && !movieTexture.isPlaying && movieTexture.isReadyToPlay) {
autoPlayed = true;
Play ();
}
//update alpha
if((isFadingIn || isFadingOut) && movieTexture.isReadyToPlay){
fadeCounter += Time.deltaTime;
if (isFadingIn) {
if (alpha < 1f) {
alpha = Mathf.Lerp(alphaStart, 1, fadeCounter/fadeInDuration);
} else {
//fadeIn complete
alpha = 1.0f;
isFadingIn = false;
}
}
if (isFadingOut) {
if(alpha>0f) {
alpha = Mathf.Lerp(alphaStart, 0, fadeCounter/fadeOutDuration);
}else{
//fadeOut complete
alpha = 0.0f;
isFadingOut = false;
}
}
}
texture.color = new Color (texture.color.r, texture.color.g, texture.color.b, alpha);
//update texture and timer
if (movieTexture.isPlaying) {
texture.texture = movieTexture;
currentTime += Time.deltaTime;
}
//verify video complete
if (isPlaying) {
//it checks if the movie has stop update, and after some frames it considers it is completed
frameFreezedCounter = currentTime == lastCurrentTime ? frameFreezedCounter+1 : 0;
if(frameFreezedCounter == frameFreezedMax) OnVideoEnd();
lastCurrentTime = currentTime;
}
//update timecode
if(TimeCodeCurrent) TimeCodeCurrent.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(currentTime));
if(TimeCodeDuration) TimeCodeDuration.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(movieTexture.duration));
}
private void OnVideoStart(){
VideoStart.Invoke ();
}
private void OnVideoEnd(){
VideoEnd.Invoke ();
if (loop) {
Stop (false);
Play (false);
} else {
Stop ();
}
}
public string Url
{
get { return _url; }
set {
_url = value;
www = new WWW(_url);
movieTexture = www.movie;
if(GetComponent<AudioSource>()) GetComponent<AudioSource>().clip = movieTexture.audioClip;
autoPlayed = false;
if(fadeIn) alpha = 0.0f;
}
}
public float CurrentTime
{
get { return currentTime; }
}
public float Duration
{
get { return movieTexture.duration; }
}
//NEW
void AddText()
{
//Need Code Here To Display Current Movie Tittle Playing???
NowPlayingText.text = display;
}
//END NEW
public void Play(bool canFadeIn = true) {
isPlaying = true;
movieTexture.Play ();
if(GetComponent<AudioSource>()) GetComponent<AudioSource>().Play ();
if(currentTime==0) OnVideoStart();
if(canFadeIn && fadeIn && alpha<1.0f) {
fadeCounter = 0;
alphaStart = alpha;
isFadingIn = true;
isFadingOut = false;
}
}
public void Stop(bool canFadeOut = true) {
isPlaying = false;
movieTexture.Stop ();
if(GetComponent<AudioSource>()) GetComponent<AudioSource>().Stop ();
currentTime = 0;
lastCurrentTime = 0;
if (canFadeOut && fadeOut) {
fadeCounter = 0;
alphaStart = alpha;
isFadingOut = true;
isFadingIn = false;
}
}
//Pause Button Option
public void Pause() {
isPlaying = false;
movieTexture.Pause ();
if(GetComponent<AudioSource>()) GetComponent<AudioSource>().Pause ();
}
//Play and Pause Button
public void TooglePlayPause() {
if (!movieTexture.isReadyToPlay) return;
if (movieTexture.isPlaying) {
Pause();
} else {
Play();
}
}
//Loop Control Toggle Button
public void Loop() {
//loop = true;
if(loop) // you can also write if(loop == true)
loop = false;
else
loop = true;
}
I'm unfamiliar with "JSON" can you elaborate a little more? Any tutorials on how it's used?
{ "movie": {
"name": "$$anonymous$$y$$anonymous$$ovie",
"length": "122.25",
"year": 2002
}
}
this is a JSON file, basic string with special char at special positions. Using parser like the one from Unify community you can parse that file into a usable object. Depending on the parser ,t eh syntax will change but you would go like:
WWW www = new WWW(url);
yield return www;
string json = www.text;
JSONObject jsonObject = JSON.Parse(str);
JSONObject image = jsonObject.GetObject("movie");
string name = image.GetString("name");
Ok Thanks fafase, How would I uses that in something like in my sample script I edited into my question. Is this two different scripts that go on different objects or are these two code snippets combined into one script?
Come on SO$$anonymous$$EONE must have a clear and concise answer on this if it is even possible??? fafase posted part of an answer but no follow up. Can the name of a movie in a URL that I am downloading to play as a movie textures name be displayed as UI text or not? And if so how? I'm getting to the point where I am just going to close this question as a CAN't BE DONE?!?
Your answer
Follow this Question
Related Questions
MovieTexture.isPlaying 2 Answers
MovieTexture in UI Panel 2 Answers
UI Text to Display Loading Progress of Songs in Music Folder 1 Answer
C# - Play a Video from a Webpage 2 Answers
On movie end 1 Answer