Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Video How to Know video player is finished playing video?

Discussion in 'Audio & Video' started by abhayaagrawal, Jul 18, 2017.

  1. abhayaagrawal

    abhayaagrawal

    Joined:
    Sep 30, 2016
    Posts:
    7
    I want to have callback when video player is finished playing video.
    If i use videoplayer.isPlaying , this won't work because this will send call back in case i want to pause video.
    there is no bool to know video is stopped or not.
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,238
  3. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    330
    @abhayaagrawal I was also looking for an answer for this and found this thread but could not really make heads or tails of it. My video only plays once so there is no looping involved. But I made up my own way of checking if the video is finished playing!

    Code (csharp):
    1.  
    2.  
    3. //Play the video!
    4. VP.GetComponent<VideoPlayer>().Play();
    5.  
    6. //Invoke repeating of checkOver method
    7. InvokeRepeating("checkOver", .1f,.1f);
    8.  
    9.  
    Code (csharp):
    1.  
    2.  
    3. //checkOver function will use current frame and total frames of video player video
    4. //to determine if the video is over.
    5.  
    6. private void checkOver()
    7. {
    8.        long playerCurrentFrame = VP.GetComponent<VideoPlayer>().frame;
    9.        long playerFrameCount = Convert.ToInt64(VP.GetComponent<VideoPlayer>().frameCount);
    10.      
    11.        if(playerCurrentFrame < playerFrameCount)
    12.        {
    13.            print ("VIDEO IS PLAYING");
    14.        }
    15.        else
    16.        {
    17.            print ("VIDEO IS OVER");
    18.           //Do w.e you want to do for when the video is done playing.
    19.        
    20.           //Cancel Invoke since video is no longer playing
    21.            CancelInvoke("checkOver");
    22.        }
    23. }
    24.  
    25.  
    Hope this helps someone else! I spent way too much time on figuring this out and figured I would share!
     
    pachermann, Marlon_Braga and smetzzz like this.
  4. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    137
    Thanks for this idea. It works well but I found that InvokeRepeating significantly slowed down my video compared to just running checkOver(); in Update. I'm not well versed in code optimization but I wonder why this is? I also wonder how much System.Covert.ToInt64 running every frame (or every Invoke) slows things down. Maybe someone who knows the engine well and code optimizations can chime in.
     
  5. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    137
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Video;
    6.  
    7. public class Scene_Intro : MonoBehaviour {
    8. public VideoPlayer vid;
    9.  
    10.  
    11. void Start(){vid.loopPointReached += CheckOver;}
    12.  
    13. void CheckOver(UnityEngine.Video.VideoPlayer vp)
    14. {
    15.      print  ("Video Is Over");
    16. }
    17.  
    18. }
    This works great too. As far as I understand it the VideoPlayer has a delegate system. vid.loopPointReached invokes the CheckOver(); function. Works, even if your video is not looping. I'm still unsure this is the most optimized way to do it. Optimization is pretty key when running HD video on mobile devices.
     
  6. nove34

    nove34

    Joined:
    Aug 22, 2017
    Posts:
    1
    Code (CSharp):
    1.     public double time;
    2.     public double currentTime;
    3.     // Use this for initialization
    4.     void Start () {
    5.  
    6.     time = gameObject.GetComponent<VideoPlayer> ().clip.length;
    7.     }
    8.  
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.         currentTime = gameObject.GetComponent<VideoPlayer> ().time;
    13.         if (currentTime >= time) {
    14.             Debug.Log ("//do Stuff");
    15.         }
    16.     }
    i usually do this
     
    JoshVIZfire, danrosero and pachermann like this.
  7. ismaelnascimento01

    ismaelnascimento01

    Joined:
    Apr 2, 2017
    Posts:
    17
    GamdineProductions likes this.
  8. pamelacook

    pamelacook

    Joined:
    Dec 13, 2017
    Posts:
    5
    videoPlayer.loopPointReached += EndReached 
    works for me!
     
  9. 54956

    54956

    Joined:
    Jan 8, 2017
    Posts:
    2
    Code (CSharp):
    1. if ((ulong)videoPlayer.frame == videoPlayer.frameCount)
    2.         {
    3.             //Video Finished
    4.         }
     
  10. metroidsnes

    metroidsnes

    Joined:
    Jan 5, 2014
    Posts:
    63
    For me the loopPointReached delegate for non-looping video wasn't triggered. Also the solution with checking frames gives me weird result.



    It worked when I casted to float instad of ulong.

    Other thing is that the VideoPlayer.frame stops a couple of frames before the frameCount. I needed to adjust for that:

    (float)m_GameIntro.frame < m_GameIntro.frameCount - 3
     
  11. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,238
  12. metroidsnes

    metroidsnes

    Joined:
    Jan 5, 2014
    Posts:
    63
    You need to cast because otherwise there's a compile error, it cannot compare ulong with long. In the picture I compare -1 to 3000 and the result is that -1 is bigger.
     
  13. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,238
    >In the picture I compare -1 to 3000
    No, you compare -1 cast to 18446744073709551615 to 3000.
    a being bigger is expected behavior.
     
  14. metroidsnes

    metroidsnes

    Joined:
    Jan 5, 2014
    Posts:
    63
    Yeah, apparently this is the result of casting -1 to ulong. Thanks for pointing this out.
     
  15. OndraPaska

    OndraPaska

    Joined:
    Jul 5, 2014
    Posts:
    1
    I think this started only with 2018.3, didn't? I suddenly need to add this, but before it was working (last frame was correctly reported)
     
  16. metroidsnes

    metroidsnes

    Joined:
    Jan 5, 2014
    Posts:
    63
    I don't how it was before. I'm having this problem with Unity 2018.3.0f1.
     
  17. DropoutGamer

    DropoutGamer

    Joined:
    Jul 3, 2019
    Posts:
    1
    @unitynoob24
    thanks man.. saved a lot of time because you