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;
      }
}



10 comments:

  1. Holy cow, thank you so much for this... I needed to figure out how to play the audio track with the movie since there is no documentation at the unity site for this... great walk through! thank goodness for google!

    ReplyDelete
  2. nice...can any one also play live stream from any web camera or cctv cam...?if you have idea then please tell or suggest some link.thanks..

    ReplyDelete
  3. 'mediasource'does not getting error....

    ReplyDelete
  4. Nayan please specify the full error u get. The code is bug free. I'll help you resolve your error

    ReplyDelete
  5. Is it possible to play the video from URL in Android Device ??

    ReplyDelete
  6. If the video is on proper webserver it will work on any device.

    ReplyDelete
  7. Because you were using Input.MouseDown() events where they are not supported in Mobile Devices...Do you have any working code for Streaming video for mobile devices ??

    ReplyDelete
  8. Try to use

    var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;


    Now you have the x,y coordinates in touchDeltaPosition

    ReplyDelete
  9. Movie textures don't work on mobile devices. Can anyone tell an alt method so it can work on iOS
    ??

    ReplyDelete
  10. I try this and I got this erro>im using Unity 4.3

    Assets/streaming.cs(52,21): error CS0103: The name `mediasource' does not exist in the current context

    ReplyDelete

News