Mik*_*ike 3 c# video unity-game-engine video-player
有没有人对 Unity 的视频播放器组件的这个问题有任何解决方案?
基本上,我有一个半交互式应用程序,可以在发生某些事情时连续播放一系列视频。问题是一旦第一个视频结束,就会有 3-5 秒的空白,因为第二个视频需要时间来加载,但这在我的应用程序中看起来不太好。
我需要一种方法来预加载第二个视频(理想)或一种相当简单的方法来隐藏间隙(例如顶部的静止帧)。对于后一种想法,我应该提到视频显示为球形。
using UnityEngine;
using UnityEngine.Video;
public class selectAndPlay_video : MonoBehaviour {
public VideoPlayer videoPlayer;
public VideoClip NewClip;
void OnEnable()
{
videoPlayer.loopPointReached += loopPointReached;
}
void OnDisable()
{
videoPlayer.loopPointReached -= loopPointReached;
}
void loopPointReached(VideoPlayer v)
{
videoPlayer.clip = NewClip;
videoPlayer.Play();
}
}
Run Code Online (Sandbox Code Playgroud)
解决这个问题的关键是VideoPlayer.Prepare()函数VideoPlayer.time和VideoPlayer.clip.length属性。首先,我建议您忘记当前使用事件播放视频的方式。像在这个问题的示例中所做的那样使用协程,因为以下答案中的所有内容都假定您在协程函数中。以下是如何播放不同的视频而无需长时间等待:
1 .有VideoClip要播放的数组/列表。
2 .VideoPlayer从#1 中的VideoClip数组创建新列表。只需使用from #1设置属性。VideoPlayer.clipVideoClips
3.调用VideoPlayer.Prepare()准备列表中的第一个 VideoPlayer,然后while循环等待,直到准备完成或VideoPlayer.isPrepared变为true。
4 .调用VideoPlayer.Play()播放您刚刚在#3 中准备的视频。
无缝地连续播放不同的视频
这是最重要的部分。
播放视频时,检查当前VideoPlayer正在播放的时间是否为视频长度的一半。如果是一半,则调用VideoPlayer.Prepare()数组中的下一个视频,然后等待#4中的视频播放完毕,然后再播放下一个视频。
通过这样做,下一个视频将在当前视频仍在播放时开始准备自己,并且该准备过程应该在当前视频播放完毕时完成。然后,您可以播放下一个视频,而无需等待它加载很长时间。
5.等待#4的视频while循环播放完毕,直到VideoPlayer.isPlaying变成false。
6.while在#5的循环内等待时,检查视频是否已播放到一半if (videoPlayerList[videoIndex].time >= (videoPlayerList[videoIndex].clip.length / 2))。如果这是真的,请调用列表中VideoPlayer.Prepare()的下一个VideoPlayer以使其准备就绪。
7 .while循环结束后,当前视频播放完毕。播放VideoPlayer列表中的下一个,然后从#5再次重复以准备VideoPlayer列表中的下一个。
下面的脚本应该完成我上面描述的所有事情。只需创建一个RawImage并将其插入图像插槽即可。您所有的所有视频也都放到 videoClipList变量中。它应该一个接一个地播放视频,而不需要很长的加载时间。该Debug.Log价格昂贵所以一旦你验证它是否工作正常删除。
//Raw Image to Show Video Images [Assign from the Editor]
public RawImage image;
//Set from the Editor
public List<VideoClip> videoClipList;
private List<VideoPlayer> videoPlayerList;
private int videoIndex = 0;
void Start()
{
StartCoroutine(playVideo());
}
IEnumerator playVideo(bool firstRun = true)
{
if (videoClipList == null || videoClipList.Count <= 0)
{
Debug.LogError("Assign VideoClips from the Editor");
yield break;
}
//Init videoPlayerList first time this function is called
if (firstRun)
{
videoPlayerList = new List<VideoPlayer>();
for (int i = 0; i < videoClipList.Count; i++)
{
//Create new Object to hold the Video and the sound then make it a child of this object
GameObject vidHolder = new GameObject("VP" + i);
vidHolder.transform.SetParent(transform);
//Add VideoPlayer to the GameObject
VideoPlayer videoPlayer = vidHolder.AddComponent<VideoPlayer>();
videoPlayerList.Add(videoPlayer);
//Add AudioSource to the GameObject
AudioSource audioSource = vidHolder.AddComponent<AudioSource>();
//Disable Play on Awake for both Video and Audio
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;
//Set Audio Output to AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
//Assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
//Set video Clip To Play
videoPlayer.clip = videoClipList[i];
}
}
//Make sure that the NEXT VideoPlayer index is valid
if (videoIndex >= videoPlayerList.Count)
yield break;
//Prepare video
videoPlayerList[videoIndex].Prepare();
//Wait until this video is prepared
while (!videoPlayerList[videoIndex].isPrepared)
{
Debug.Log("Preparing Index: " + videoIndex);
yield return null;
}
Debug.LogWarning("Done Preparing current Video Index: " + videoIndex);
//Assign the Texture from Video to RawImage to be displayed
image.texture = videoPlayerList[videoIndex].texture;
//Play first video
videoPlayerList[videoIndex].Play();
//Wait while the current video is playing
bool reachedHalfWay = false;
int nextIndex = (videoIndex + 1);
while (videoPlayerList[videoIndex].isPlaying)
{
Debug.Log("Playing time: " + videoPlayerList[videoIndex].time + " INDEX: " + videoIndex);
//(Check if we have reached half way)
if (!reachedHalfWay && videoPlayerList[videoIndex].time >= (videoPlayerList[videoIndex].clip.length / 2))
{
reachedHalfWay = true; //Set to true so that we don't evaluate this again
//Make sure that the NEXT VideoPlayer index is valid. Othereise Exit since this is the end
if (nextIndex >= videoPlayerList.Count)
{
Debug.LogWarning("End of All Videos: " + videoIndex);
yield break;
}
//Prepare the NEXT video
Debug.LogWarning("Ready to Prepare NEXT Video Index: " + nextIndex);
videoPlayerList[nextIndex].Prepare();
}
yield return null;
}
Debug.Log("Done Playing current Video Index: " + videoIndex);
//Wait until NEXT video is prepared
while (!videoPlayerList[nextIndex].isPrepared)
{
Debug.Log("Preparing NEXT Video Index: " + nextIndex);
yield return null;
}
Debug.LogWarning("Done Preparing NEXT Video Index: " + videoIndex);
//Increment Video index
videoIndex++;
//Play next prepared video. Pass false to it so that some codes are not executed at-all
StartCoroutine(playVideo(false));
}
Run Code Online (Sandbox Code Playgroud)