C# 按键时使用Unity VideoPlayer缓慢暂停/恢复视频
我正在编写一个脚本,其中视频分为3部分,分别有3个音频,在前一部分完成后运行(视频和音频),但是视频只需要在按住键(在我的情况下是“空格”)时播放。我成功地在按下空格键的同时播放了视频和音频:C# 按键时使用Unity VideoPlayer缓慢暂停/恢复视频,c#,unity3d,video-player,C#,Unity3d,Video Player,我正在编写一个脚本,其中视频分为3部分,分别有3个音频,在前一部分完成后运行(视频和音频),但是视频只需要在按住键(在我的情况下是“空格”)时播放。我成功地在按下空格键的同时播放了视频和音频: if (Input.GetButton ("Jump")) { vp.Play (); if (!ASource.isPlaying) { ASource.Play ();
if (Input.GetButton ("Jump")) {
vp.Play ();
if (!ASource.isPlaying) {
ASource.Play ();
}
并暂停,而不是:
else {
vp.Pause ();
ASource.Pause ();
}
但这是一个艰难的过程,我正在寻找一种方法使它平稳地暂停/恢复,我尝试了一个功能:
public void videoPause() {
for (i = 0; i < 10; i++) {
vp.playbackSpeed = vp.playbackSpeed / i;
}
}
public void videoPause(){
对于(i=0;i<10;i++){
vp.playbackSpeed=vp.playbackSpeed/i;
}
}
简历也一样,但是用*代替了和我--但它不起作用,你知道如何使它起作用吗?见此:
for (i = 0; i < 10; i++) {
vp.playbackSpeed = vp.playbackSpeed / i;
}
在启动新的协同程序之前,您需要确保上一个协同程序函数没有运行。停止旧的,然后在需要时启动新的。以下两个函数应能处理该问题,并能安全地调用上述函数:
Coroutine pauseCoroutine;
Coroutine resumeCoroutine;
bool executingPause = false;
void SmoothlyPauseOverTime(VideoPlayer targetVp, float duration)
{
//Stop old coroutines before starting a new one
if (pauseCoroutine != null)
StopCoroutine(pauseCoroutine);
if (resumeCoroutine != null)
StopCoroutine(resumeCoroutine);
executingPause = true;
pauseCoroutine = StartCoroutine(SmoothlyPauseOverTimeCOR(targetVp, duration));
}
void SmoothlyResumeOverTime(VideoPlayer targetVp, float duration)
{
if (pauseCoroutine != null)
StopCoroutine(pauseCoroutine);
//Stop old coroutines before starting a new one
if (resumeCoroutine != null)
StopCoroutine(resumeCoroutine);
resumeCoroutine = StartCoroutine(SmoothlyResumeOverTimeCOR(targetVp, duration));
}
您的更新
功能:
void Update()
{
if (Input.GetButton("Jump"))
{
if (!vp.isPlaying)
{
Debug.Log("Resumed Playing");
SmoothlyResumeOverTime(vp, 0.8f);
}
}
else
{
if (!executingPause && vp.isPlaying)
{
Debug.Log("Paused Playing");
SmoothlyPauseOverTime(vp, 0.8f);
}
}
}
public VideoPlayer vp;
您的启动
功能:
void Update()
{
if (Input.GetButton("Jump"))
{
if (!vp.isPlaying)
{
Debug.Log("Resumed Playing");
SmoothlyResumeOverTime(vp, 0.8f);
}
}
else
{
if (!executingPause && vp.isPlaying)
{
Debug.Log("Paused Playing");
SmoothlyPauseOverTime(vp, 0.8f);
}
}
}
public VideoPlayer vp;
基于。它准备视频,但直到在上面的Update
功能中按下空格键后才播放:
//Raw Image to Show Video Images [Assign from the Editor]
public RawImage image;
//Video To Play [Assign from the Editor]
public VideoClip videoToPlay;
private VideoPlayer vp;
private VideoSource videoSource;
//Audio
private AudioSource aS;
// Use this for initialization
void Start()
{
Application.runInBackground = true;
StartCoroutine(playVideo());
}
IEnumerator playVideo()
{
//Add VideoPlayer to the GameObject
vp = gameObject.AddComponent<VideoPlayer>();
//Add AudioSource
aS = gameObject.AddComponent<AudioSource>();
//Disable Play on Awake for both Video and Audio
vp.playOnAwake = false;
aS.playOnAwake = false;
//We want to play from video clip not from url
vp.source = VideoSource.VideoClip;
//Set Audio Output to AudioSource
vp.audioOutputMode = VideoAudioOutputMode.AudioSource;
//Assign the Audio from Video to AudioSource to be played
vp.EnableAudioTrack(0, true);
vp.SetTargetAudioSource(0, aS);
//Set video To Play then prepare Audio to prevent Buffering
vp.clip = videoToPlay;
vp.Prepare();
//Wait until video is prepared
while (!vp.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}
Debug.Log("Done Preparing Video");
//Assign the Texture from Video to RawImage to be displayed
image.texture = vp.texture;
}
将在0.8秒内缓慢暂停
SmoothlyPauseOverTime(vp, 0.8f);
SmoothlyResumeOverTime(vp, 0.8f);
将在0.8秒内缓慢恢复
SmoothlyPauseOverTime(vp, 0.8f);
SmoothlyResumeOverTime(vp, 0.8f);
videoPause中的for循环方法将一次全部运行,而不是按您希望的方式平稳运行。您可以尝试在更新方法中更新播放速度,时间为5秒。像这样的
// set when the pause is triggered
videoPauseTime = Time.realtimeSinceStartup;
// duration in seconds to smoothen the pause
duration = 5
public void Update() {
float t = (Time.realtimeSinceStartup - videoPauseTime) / duration;
if(t>1.0f)
t = 1.0f;
vp.playbackSpeed = Mathf.Lerp (vp.playbackSpeed,0.0f,t)
}
请注意,此代码仅用于说明,t将超出1.0,这是您不想要的;因此,条件t>1.0f。另外,我假设playbackSpeed为0会使视频停止播放。不是在我的Unity计算机上进行测试,但这应该可以让您开始。我希望它能编译。如果没有,请修复小问题。谢谢,它在inspector中工作(播放速度“animate”为0或1),但在视频中有点奇怪,它等待“速度动画”完成,然后视频恢复,这意味着它没有缓慢地恢复它只是开始,因为暂停正常(暂停前它变慢),知道为什么吗?如果需要此帮助,请观看360视频。请参阅更新。让我知道这是否解决了您当前的问题。由于按下空格是在更新功能中完成的,因此现在恢复有点循环,并且视频没有恢复。在调用该功能之前,您还必须确保视频没有播放
if(Input.GetButton(“Jump”){if(!vp.isPlaying){smoothlyresumetowertime(vp,0.8f);}else{if(vp.isPlaying){SmoothlyPauseOverTime(vp,0.8f);}}}
用Update
函数中的代码替换它。