检测 VideoPlayer 何时播放完毕
Detect when VideoPlayer has finished playing
我有一个 MovieController class 来管理我项目中的视频。我正在使用 Unity 5.6 中引入的新视频播放器组件。
我想在电影播放完毕后调用一个方法。到目前为止,这个方法只有一个Debug.Log
,可以看到:
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
在视频结束时没有被调用,我只是以黑屏结束,控制台中没有任何内容。
我不信任或不使用 VideoPlayer
API 中的 事件 ,因为我 运行 也遇到过它们的问题.您看到的是一个数月未修复的错误。
如果您想检测视频是否已播放完毕,请使用协程在 while 循环中检查 videoPlayer.isPlaying
每一帧。当while循环存在时,表示视频播放完毕。
while (videoPlayer.isPlaying)
{
yield return null;
}
//Done Playing
我现在无法测试暂停功能是否影响videoPlayer.isPlaying
,但您可以自行测试。
另一种方法是检查是否 frame
is the-same as frameCount
。当这是真的时,你就知道视频已经播放完了。
if(videoPlayer.frame == videoPlayer.frameCount)
{
//Video has finshed playing!
}
如果其中 none 个解决了您的问题,请提交错误报告。
有趣的是,这在 Unity 2018.4.25(我正在使用的当前版本)中仍然不能完全按预期工作。在启动时播放视频通常会导致 isPlaying 为 false(跳出视频而不播放,同时测试
(videoPlayer.frame == videoPlayer.frameCount)
在移动设备上似乎无法可靠地工作 (android),因为视频会在播放完所有帧(通常是几个短帧,我假设是跳帧)。
我找到了
if (( videoPlayer.frame) > 0 && (videoPlayer.isPlaying == false))
捕获了我在移动设备上 运行 遇到的用例问题,但是可能还有更多有趣的用例和组合需要处理。我将上面的测试放入 update() 而不是协程。
HTH.
using UnityEngine.Video;
private VideoPlayer vid;
private float beginning;
void Awake()
{
vid = GetComponent<VideoPlayer>();
beginning = Time.time;
vid.Play();
StartCoroutine(waitmethod());
}
IEnumerator waitmethod()
{
while (Time.time - beginning < vid.length)
{
yield return null;
}
runyournext();
}
您可以使用
videoPlayer.loopPointReached += OnMovieFinished;
//the action on finish
void OnMovieFinished(UnityEngine.Video.VideoPlayer vp)
{
vp.playbackSpeed = vp.playbackSpeed / 10.0F;
}
你可以在这里看到更多https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
我有一个 MovieController class 来管理我项目中的视频。我正在使用 Unity 5.6 中引入的新视频播放器组件。
我想在电影播放完毕后调用一个方法。到目前为止,这个方法只有一个Debug.Log
,可以看到:
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
在视频结束时没有被调用,我只是以黑屏结束,控制台中没有任何内容。
我不信任或不使用 VideoPlayer
API 中的 事件 ,因为我 运行 也遇到过它们的问题.您看到的是一个数月未修复的错误。
如果您想检测视频是否已播放完毕,请使用协程在 while 循环中检查 videoPlayer.isPlaying
每一帧。当while循环存在时,表示视频播放完毕。
while (videoPlayer.isPlaying)
{
yield return null;
}
//Done Playing
我现在无法测试暂停功能是否影响videoPlayer.isPlaying
,但您可以自行测试。
另一种方法是检查是否 frame
is the-same as frameCount
。当这是真的时,你就知道视频已经播放完了。
if(videoPlayer.frame == videoPlayer.frameCount)
{
//Video has finshed playing!
}
如果其中 none 个解决了您的问题,请提交错误报告。
有趣的是,这在 Unity 2018.4.25(我正在使用的当前版本)中仍然不能完全按预期工作。在启动时播放视频通常会导致 isPlaying 为 false(跳出视频而不播放,同时测试
(videoPlayer.frame == videoPlayer.frameCount)
在移动设备上似乎无法可靠地工作 (android),因为视频会在播放完所有帧(通常是几个短帧,我假设是跳帧)。
我找到了
if (( videoPlayer.frame) > 0 && (videoPlayer.isPlaying == false))
捕获了我在移动设备上 运行 遇到的用例问题,但是可能还有更多有趣的用例和组合需要处理。我将上面的测试放入 update() 而不是协程。
HTH.
using UnityEngine.Video;
private VideoPlayer vid;
private float beginning;
void Awake()
{
vid = GetComponent<VideoPlayer>();
beginning = Time.time;
vid.Play();
StartCoroutine(waitmethod());
}
IEnumerator waitmethod()
{
while (Time.time - beginning < vid.length)
{
yield return null;
}
runyournext();
}
您可以使用
videoPlayer.loopPointReached += OnMovieFinished;
//the action on finish
void OnMovieFinished(UnityEngine.Video.VideoPlayer vp)
{
vp.playbackSpeed = vp.playbackSpeed / 10.0F;
}
你可以在这里看到更多https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html