gpt4 book ai didi

c# - 使用 Unity Video Player 连续无缝播放不同的视频

转载 作者:行者123 更新时间:2023-11-30 20:28:27 52 4
gpt4 key购买 nike

对于这个关于 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();
}
}

最佳答案

解决这个问题的关键是VideoPlayer.Prepare()函数,VideoPlayer.timeVideoPlayer.clip.length特性。首先,我确实建议您忘记当前使用事件播放视频的方式。使用来自 this 的示例中的协程问题,因为下面答案中的所有内容都假定您在协程函数中。以下是如何播放不同的视频而无需长时间等待:

1。有要播放的 VideoClip 数组/列表。

2。从 #1 中的 VideoClip 数组创建新的 VideoPlayer 列表。只需使用 #1 中的 VideoClips 设置 VideoPlayer.clip 属性。

3.调用VideoPlayer.Prepare()准备列表中的第一个VideoPlayer然后等待while 循环,直到准备完成或 VideoPlayer.isPrepared 变为 true

4.调用VideoPlayer.Play()播放您刚刚在#3中准备的视频。

Play different videos back to back seamlessly

这是最重要的部分。

播放视频时,检查正在播放的VideoPlayer的当前时间是否是视频长度的一半。如果播放到一半,则对数组中的下一个视频调用 VideoPlayer.Prepare(),然后等待来自 #4 的视频播放完毕,然后再播放下一个视频。

通过这样做,下一个视频将在当前视频仍在播放时自行开始准备,并且准备过程应该在当前视频播放完毕时完成。然后您可以播放下一个视频,而无需长时间等待它加载。

5。等待来自 #4 的视频在 while 循环中完成播放,直到 VideoPlayer.isPlaying变为 false

6。在 #5while 循环中等待时,使用 if ( videoPlayerList[videoIndex].time >= (videoPlayerList[videoIndex].clip.length/2))。如果这是真的,请在列表中的下一个 VideoPlayer 上调用 VideoPlayer.Prepare() 以使其准备就绪。

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));
}

关于c# - 使用 Unity Video Player 连续无缝播放不同的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47401759/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com