Search code examples
c#videounity-game-enginevideo-streamingtextures

How to apply and play a video from the Unity Streaming Assets folder as a texture (MovieTexture)


I am trying to get a video (.ogv) from the Streaming Assets folder, apply it on to a surface and play it. (C# code added below) - Unity 5 Personal

void Start () {
 private MovieTexture myMoviePlayerTexture;

 string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "test.ogv");

 WWW www = new WWW("file:///" + filePath);
 Renderer r = GetComponent<Renderer>();
 r.material.mainTexture = www.movie;
 www.movie.Play();
}

C# Script added to a simple plane.

However when I add the MovieTexture to a plane (from assets folder) it plays without any problems.

((MovieTexture)GetComponent<Renderer>().material.mainTexture).Play();

edit: the Problem: the video does not play from the Streaming Assets folder, it appears as if a material is loaded but it does not play.

Any help appriciated. Thanks.

EDIT

This is the game view: link and this is the folder structure link nothing fancy just some test videos


Solution

  • The code in your question is not even compiling. There are so many errors.

    Anyways, don't play the video directly from the WWW object with www.movie.Play();. Get the MovieTexture from WWW first then play the video from there. Also, check if MovieTexture.isReadyToPlay is true before playing the video. You can do this in a coroutine or the Update() function.

    Make sure that the test.ogv video is in the StreamingAssets folder. Your screenshot does not show what's inside the StreamingAssets folder.

    Finally, playing video on a plane is now old. You can play Video on Unity's RawImage which is easier to work with in Unity.

    I can't tell which Unity 5 version you are using but please update to 5.5 or above then use the code below:

    MovieTexture myMoviePlayerTexture;
    void Start()
    {
        string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "test.ogv");
    
        WWW www = new WWW("file:///" + filePath);
        Renderer r = GetComponent<Renderer>();
        myMoviePlayerTexture = www.GetMovieTexture();
    
        r.material.mainTexture = myMoviePlayerTexture;
    }
    
    void Update()
    {
        if (myMoviePlayerTexture.isReadyToPlay && !myMoviePlayerTexture.isPlaying)
        {
            myMoviePlayerTexture.Play();
        }
    }
    

    It's worth noting that MovieTexture is now deprecated. See this post for the newsiest method to play video.