- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于这个关于 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.time
和VideoPlayer.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。在 #5 的 while
循环中等待时,使用 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/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!