How to rewind a video player properly?
I am attempting to rewind a VideoPlayer in Unity, also I am using the new VideoPlayer API and an mp4. I have tried setting the playback speed to a negative number, but it pauses. My current solution is, In my rewind button script:
void Update ()
{
if (rewind == true) {
VideoController.Backward2Seconds();
}
}
In my VideoController Script
public void Backward2Seconds() {
if (!IsPlaying) return;
videoPlayer.time = videoPlayer.time - 2;
}
Is there a better way? Because this is laggy.
Answer by rdibaje · Sep 18, 2019 at 08:25 AM
Technically, "videoPlayer.time = 0;" will return the video to the start, but if you want to control how much to rewind, you can declare a speed variable and just deduct that from the current time of the video. The following code worked for my project:
private float speed = 2f;
public void Rewind()
{
videoPlayer.time -= speed;
}
Answer by Rs · Sep 21, 2021 at 10:03 PM
If by rewind you mean reset to time = 0 and replay the video, I have this in a few projects and it never failed. In your case you want to use the first public method here.
public void PlayCurrentVideoClip()
{
Assert.IsNotNull(videoPlayer.clip);
PlayVideoClip(videoPlayer.clip);
}
public void PlayVideoClip(VideoClip _vc) {
StartCoroutine(PlayVideoClipCoroutine(_vc));
}
IEnumerator PlayVideoClipCoroutine(VideoClip _vc) {
videoPlayerUI?.SetActive(true);
yield return new WaitForEndOfFrame(); // safely wait 1 frame for the UI to be active
SetVideoClip(_vc);
videoPlayer.enabled = true;
videoPlayer.frame = 0;
videoPlayer.Play();
videoPlayer.EnableAudioTrack(0, true);
OnPlay.Invoke();
}
The only downside is that you might see a bit of a glitch where the video panel disappears for a split second. Can be annoying but not the end of the world.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144929im_/https://answers.unity.com/themes/thub/images/avi.jpg)