3iX

Reliable $1 Web Hosting by 3iX

Wednesday, May 12, 2010

Play Streaming Video in Unity 3D

Video can be streamed in Unity3D only when it is in ogg or ogv format. To get ogv file

  1. put any video file (mov, avi, mp4 ) inside unity 3d project window. 
  2. Right Click the movie file and choose export ogg file
  3. Save the file in Desktop or some known location
  4. setup your web server both remote and local web server will work fine. I'll go for local web server I'll get and install xampp from apache friends
  5. Copy paste the exported files i.e. the ogv files in the xampp htdocs folder such that it will be accessed from browser as http://localhost/filename.ogv
Now use the following code as cs file to play videos

using UnityEngine;
using System.Collections;

public class streaming : MonoBehaviour {

private MovieTexture  m;
private string playButtonString="play",url= "http://localhost/";
private string [] mediaSource = {"1.ogv","2.ogv","3.ogv","4.ogv","5.ogv"};
public AudioSource AS;
bool isMenu = true;
int tmp;
private WWW www;

// Use this for initialization

void Start () {
 
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Escape))

{
//~ Screen.showCursor = true;
Screen.fullScreen = false;
isMenu = true;
}
if(Screen.fullScreen == true) {
if(Input.mousePosition.x >=20 && Input.mousePosition.x<=(Screen.width -40) && Input.mousePosition.y>=0 && Input.mousePosition.y <= 80)
isMenu = true;
else
isMenu = false;
}
else
isMenu = true;
}
void OnGUI() {
if(m!=null)
GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),m,ScaleMode.StretchToFill);
else
GUI.Label(new Rect(50,100,200,50), "Video is yet to Start");
if(isMenu) {
GUI.Box(new Rect(20, Screen.height - 60, Screen.width-40,60),"");
for ( int i=0;i < mediasource.length;i++) {
if(m!=null)
if (i == tmp && m.isPlaying) 
playButtonString="pause"; //it's playing so the button should pause.
else
playButtonString="play"; //it's not playing and the button should play the movie.
if (GUI.Button (new Rect (30 + (200 * i),Screen.height-40,100,30),playButtonString + i) == true)
{
if(m!=null)
if (m.isPlaying == true) {
m.Pause();
AS.Pause();
if (i == tmp)
return;
}
else if(m.isReadyToPlay && i == tmp)
{
m.Play();
AS.Play();
return;
}
www = new WWW(url+mediaSource[i]);
m = www.movie;
AS.clip = m.audioClip;
while(!m.isReadyToPlay) {
SomeCoroutine();
if (!m.isPlaying) {
m.Play();
if(!AS.isPlaying)
AS.Play();
}
}
Screen.fullScreen = true;
isMenu = false;
tmp = i;
}
if (GUI.Button (new Rect(130 + (200 *i),Screen.height-40,100,30),"Stop"+ i)==true && i==tmp) {
m.Stop();
AS.Stop();
}
}
}
else {
GUI.Label( new Rect(Screen.width - 200, 10,200,20), "Press Esc for Menu or");
if(GUI.Button( new Rect(Screen.width - 200, 30,100,20), "Click Here")) {
isMenu = true;
Screen.fullScreen = false;
}
}
}
IEnumerator SomeCoroutine () {
        // Wait for one frame
        yield return www;
      }
}



News