How to know if a VideoPlayer has ended (using VideoPlayer .isPlaying inside a Courutine)?

Hi, i’m trying to do something when a VideoPlayer has ended (well, it’s VideoClip) so I used a Courutine but the variable that has to be true only when the Video Player ends begins in true and I don’t know why is this happenning. It supose that isVideoFinished should be false until the while’s condition is false.


Original Post on Discord

by user 632418299711324161

using UnityEngine;
using UnityEngine.Video;

public class VideoPlayerController : MonoBehaviour
{
public VideoPlayer videoPlayer;

private void Start()
{
    // Assuming you have a reference to the VideoPlayer component
    videoPlayer.loopPointReached += OnVideoEnd;
}

private void OnVideoEnd(VideoPlayer vp)
{
    // Video has ended
    Debug.Log("Video ended");
}

// Alternatively, you can also check the isPlaying property
private void Update()
{
    if (!videoPlayer.isPlaying)
    {
        // Video has ended
        Debug.Log("Video ended");
    }
}

}

by user 267660755443122181

Thank you but I mean, using typescript for a needle in script/src . In needle there are no events for the component VideoPlayer (like .loopPoinReached) and neither the “length” property.

by user 632418299711324161

Hey :wave: do i understand your issue correctly that the .isPlaying never gets to equal to false?

Do you have the loop turned off? Just double check that the flag works as expected, maybe something specific to file format?

Or is your usecase a looping video and you would like to trigger code when it loops around?

VideoPlayer is a wrapper above the video html element. (https://www.w3schools.com/tags/ref_av_dom.asp)
And you can access that via videoPlayer.videoElement and there call js api.

I think you could try hooking up to the ended event and that perhaps gets called on video loop round.

Thank you! It Works ! It really works, I didn’t know about the .videoElement, I don´t know why the event ended works better than the isPlaying but it works. And thank uo for the link :grin:

by user 632418299711324161

Glad to help :slight_smile: Can you please specify in what case the isPlaying is not working so it could get fixed?

Was the video looping or not?

What file format have you used, etc.

What Unity version and are you on the most up to date version of needle?

Yea sure, let me organize it and I send you :slightly_smiling_face:

by user 632418299711324161

Issue : A class 's static boolean attribute change inside a Generator(from typescript or a Courutine in C#) when It suppose not to change unless the VideoPlayer.isPlaying = false.
About VideoPlayer component : Play On Awake : True , Wait For First Frame : False, Loop : False Skip On Drop : True
Video Format : .mp4 with Override for EmbeddedLinux : checked
Unity Version : Unity 2021.3.18f1 Personal
Needle Engine Version :Exporter 3.7.3-exp Engine 3.7.3 - alpha

Solved : Using VideoPlayer.videoElement.ended event from HTML Audio/Video DOM Reference

by user 632418299711324161

Hey :wave: what do you mean by a Generator in this context?

I think he maybe refers to JavaScript generators (thats what unity calls a coroutine basically)