VideoPlayer.frameReady
Параметры
| Параметр | Описание |
|---|---|
| значение | Номер готового кадра (нулевой индекс). |
Описание
VideoPlayer вызывает это событие, когда новый кадр готов для отображения.
Используйте это событие для:
- Проанализировать определенные кадры видео.
- Отслеживание прогресса видео.
- Воспроизвести другие эффекты, такие как анимации или звуковые эффекты на определенном кадре.
Чтобы разрешить это событие, чтобы VideoPlayer выпустил его, установите свойство VideoPlayer.sendFrameReadyEvents на true. Это событие, вероятно, обложит CPU, поэтому установите VideoPlayer.sendFrameReadyEvents обратно на false, когда оно вам не нужно.
VideoPlayer также выпустит это событие, если вы вызовете VideoPlayer.Pause на VideoPlayer, который еще не подготовлен или не воспроизводится в настоящее время. Когда вы вызовете Pause() на VideoPlayer, который не подготовлен или не воспроизводится, он ведет себя так, как если бы вы вызвали Play() и затем немедленно вызвали Pause(). Это позволяет вам искать определенную точку в видео и сделать паузу, чтобы дать ему время подготовить кадр перед проигрыванием.
// This script plays some audio when the VideoPlayer reaches the frame (targetFrame) you set. // Make sure to assign a VideoPlayer component to your GameObject and assign an AudioSource in the Inspector.
using UnityEngine; using UnityEngine.UIElements; using UnityEngine.Video;
public class FrameReadyExample : MonoBehaviour { VideoPlayer videoPlayer; public AudioSource audioSource;
// The frame you want to play the sound at (set this value in the Inspector). public int targetFrame;
void Start() { videoPlayer = GetComponent<VideoPlayer>();
if (videoPlayer != null) { // Prepare the VideoPlayer to play the video. videoPlayer.prepareCompleted += OnPrepareCompleted; videoPlayer.Prepare(); } else Debug.LogWarning("Your GameObject doesn't have a VideoPlayer component."); }
void OnPrepareCompleted(VideoPlayer vp) { // Clamp targetFrame to be within the frame count of the video. var totalFrames = videoPlayer.frameCount; targetFrame = Mathf.Clamp(targetFrame, 0, (int)totalFrames - 1);
videoPlayer.sendFrameReadyEvents = true; // When frameReady event is invoked, call this function. videoPlayer.frameReady += OnFrameReady;
videoPlayer.Play(); }
void OnFrameReady(VideoPlayer vp, long frameToPlay) { Debug.Log("Frame " + frameToPlay + " is ready.");
// Play the audio when the VideoPlayer video reaches the target frame. if (frameToPlay == targetFrame) { if (audioSource != null) { audioSource.Play(); } else Debug.LogWarning("AudioSource component is missing."); videoPlayer.sendFrameReadyEvents = false; } } }