- Home /
C# - Play a Video from a Webpage
Hello,
I´m trying to play a video from a webpage. I tried to do it with a MovieTexture through creating a plane, giving a guiTexture to it and giving it the following script:
using UnityEngine;
using System.Collections;
public class ViewStream : MonoBehaviour {
GameObject myGameObject;
public WWW wwwData;
public string url = "http://www.youtube.com/watch?v=tkwwrTW7BQU&feature=g-vrec";
// Use this for initialization
void Start () {
wwwData = new WWW(url);
//guiTexture.texture = wwwData.movie;
renderer.material.mainTexture = wwwData.movie;
}
// Update is called once per frame
void Update () {
//MovieTexture m = guiTexture.texture as MovieTexture;
MovieTexture m = renderer.material.mainTexture as MovieTexture;
if(m.isReadyToPlay==true){
print ("ready to play");
}else{
print ("not ready to play");
}
if (!m.isPlaying && m.isReadyToPlay){
m.Play();
}
}
}
First I tried it with the guiTexture. After it didn´t work I tried to use the main material to play it. But this failed also. In the log is written "not ready to play" all the time.
Can anyone tell me what I´m doing wrong? Does the video file need to have a special format, is my URL not correct or anything else?
Thank you in advance.
Answer by Graham-Dunnett · Oct 05, 2012 at 08:31 PM
Just use the documentation... The example for WWW.movie does what you want.
http://docs.unity3d.com/Documentation/ScriptReference/WWW-movie.html
What you are doing wrong (I am guessing) is YouTube is not giving you an Ogg movie, so it's never ready to play.
Hello,
thank you for your response. Now I´m able to load and play the example video of Unity. But I still have a problem. I´m using a while loop to say if the video is not ready to play unity should wait for some seconds and then check again. The problem is even if the video is not loaded completely Unity recognizes that a part of the video is already ready to play. So he starts playing the video until the unloaded point. When he reaches the unloaded point in the video then he is crashing.
Do you know a way to wait until the video is loaded completely?
$$anonymous$$y while loop looks like that:
[code]
void Start () {
wwwData = new WWW(url);
$$anonymous$$ovieTexture m = wwwData.movie;
while (!m.isReadyToPlay){
StartCoroutine(WaitAndPrint(50.0F));
}
//...
}
IEnumerator WaitAndPrint(float waitTime) {
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
}
[/code]
Try this:
void Start(){
StartCoroutine(StartVideo());
}
IEnumerator StartVideo() {
wwwData = new WWW(url);
yield return wwwData;
$$anonymous$$ovieTexture m = wwwData.movie;
while (!m.isReadyToPlay){
print("WaitAndPrint " + Time.time);
}
}
Hi Nanashi91, I see that you could play videos from the youtube web, I want to ask you in the end how did you do it? Because I have the same problem and here only you have placed that you succeeded, if you could help I would thank you very much, please
Have you read the answer? You can not play Youtube videos as Youtube actually streams it in chunks in their own format. Youtube doesn't offer the videos as "single file downloads".
Besides that the Youtube GTCs (general terms and conditions) do not allow this use:
4) A:
You agree not to distribute in any medium any part of the Service or the Content without YouTube's prior written authorization, unless YouTube makes available the means for such distribution through functionality offered by the Service (such as the Embeddable Player).
4) C:
You agree not to access Content through any technology or means other than the video playback pages of the Service itself, the Embeddable Player, or other explicitly authorized means YouTube may designate.
If you try to load / display videos manually from Youture you would violating their terms of service. So it's legally not allowed to do so, even when it was possible.
Answer by Zero55 · Feb 22, 2013 at 12:18 PM
Unity only supports .ogv and .ogg as video and audio format for the web player.
Try this link to download your youtube video as .MP4 http://www.youtubeinmp4.com
and then go to this link to convert and download the video.
http://video.online-convert.com/convert-to-ogg
Have fun!