Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Video - controls by script

Discussion in 'Project Tiny' started by vitaly_yavorsky, May 29, 2019.

  1. vitaly_yavorsky

    vitaly_yavorsky

    Joined:
    Oct 6, 2017
    Posts:
    2
    Hi,
    By documentation VideoPlayer component doesn't have any methods to pause and resume playback. Is there any way to achieve playback control by code?
     
  2. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
    Unfortunately, videos in Unity Tiny are not fully integrated and lack some utilities. Unity Tiny currently simply makes use of HTML's video element. You are right to notice that the VideoPlayer is not pausable via the component. However, you can locate the html video element corresponding to your entity. You can simply look up the entity via the html-document.

    The second part is to actually pause and resume the video. When you follow this link you can find out more about the html video element, and here is an example, including pausing and resuming the video.

    Hope this helps!
     
    vitaly_yavorsky likes this.
  3. vitaly_yavorsky

    vitaly_yavorsky

    Joined:
    Oct 6, 2017
    Posts:
    2
    Thank you, Zionisias, for helpful information!
     
    Zionisias likes this.
  4. Nimdanet

    Nimdanet

    Joined:
    Jan 19, 2013
    Posts:
    38
    Hi! For a more elaborated solution, you could do something like this:

    We already know for now unity only creates a HTML's video element. We can get a specific video searching all video elements and comparing its "src" property.

    Problems: although if you inspect html on browser and src would be something like: "Assets/SampleVideo.mp4", inside your code it returns something like: "http://localhost:19050/Assets/SampleVideo.mp4" (Is this a bug?). So comparing directly with "==" would return false. My workaround was to get indexOf >= 0.

    So I created a function that returns a HTMLVideoElement. You can make this static if you want:

    Code (CSharp):
    1. private findHTMLVideoElement(src: string): HTMLVideoElement
    2. {
    3.     let videoElements = document.getElementsByTagName("video");
    4.     for (let i = 0; i < videoElements.length; i++)
    5.     {
    6.         const element = videoElements[i];
    7.      
    8.         if (element.src.indexOf(src) >= 0)
    9.         {
    10.             return element;
    11.         }
    12.     }
    13. }
    This HTMLVideoElement has play() and pause() methods.

    On your main code you could use something like this:

    this.findHTMLVideoElement("Assets/SampleVideo.mp4").play()

    this.findHTMLVideoElement("Assets/SampleVideo.mp4").pause()


    I hope this helps a little more :)

    EDIT: video tag is not created on "OnEnable" or the first few frames the application is running. Please check
    this.findHTMLVideoElement("Assets/SampleVideo.mp4") != undefined
    before using any code or it will prompt an error.
     
    Last edited: Oct 18, 2019