Detect when VideoPlayer has finished playing

北慕城南 提交于 2020-01-11 09:44:19

问题


I have a MovieController class that manages videos in my project. I'm using the new video player component introduced in Unity 5.6.

I would like to call a method when a movie has finished playing. So far, this method is only a Debug.Log, as you can see:

using UnityEngine;
using UnityEngine.Video;

public class MovieController : MonoBehaviour
{
    private VideoPlayer m_VideoPlayer;

    void Awake () 
    {
        m_VideoPlayer = GetComponent<VideoPlayer>();
        m_VideoPlayer.loopPointReached += OnMovieFinished; // loopPointReached is the event for the end of the video
    }

    void OnMovieFinished(VideoPlayer player)
    {
        Debug.Log("Event for movie end called");
        player.Stop();
    }
}

My issue is that the OnMovieFinished is not called at the end of the video, and I just end up with a black screen and nothing in the console.


回答1:


I don't trust or use the events from the VideoPlayer API because I've ran into problems with them too. What you see is a bug that hasn't been fixed for months.

If you want to detect if video has finished playing, use a coroutine to to check videoPlayer.isPlaying every frame in a while loop. When the while loop exists, it means that the video has finished playing.

while (videoPlayer.isPlaying)
{
    yield return null;
}

//Done Playing

I can't test this right now to check if the pause function affects videoPlayer.isPlaying but you can test that by yourself.


Another way to do this is to check if frame is the-same as frameCount. When this is true, you know that the video has finished playing.

if(videoPlayer.frame == videoPlayer.frameCount)
{
    //Video has finshed playing!
}

If none of these solved your problem then file for a bug report.



来源:https://stackoverflow.com/questions/44696030/detect-when-videoplayer-has-finished-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!