检测VideoPlayer何时播放完毕 [英] Detect when VideoPlayer has finished playing
问题描述
我有一个MovieController类,用于管理项目中的视频.我正在使用Unity 5.6中引入的新视频播放器组件.
I have a MovieController class that manages videos in my project. I'm using the new video player component introduced in Unity 5.6.
我想在电影播放完后调用一个方法.到目前为止,如您所见,此方法只是一个Debug.Log
:
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();
}
}
我的问题是OnMovieFinished
在视频末尾没有被调用,而我最终只是黑屏,而控制台中什么也没有.
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.
推荐答案
我不信任或使用VideoPlayer
API中的事件,因为我也遇到了问题.您看到的是一个已经几个月没有修复的错误.
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.
如果要检测视频是否已完成播放,请使用协程检查while循环中的每帧videoPlayer.isPlaying
.当while循环存在时,表示视频已播放完毕.
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
我现在无法测试此功能,以检查暂停功能是否影响videoPlayer.isPlaying
,但您可以自己进行测试.
I can't test this right now to check if the pause function affects videoPlayer.isPlaying
but you can test that by yourself.
另一种方法是检查 frame
与 frameCount
相同.当这是真的时,您就知道视频已经播放完了.
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.
这篇关于检测VideoPlayer何时播放完毕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!