问题
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