Search Unity

Video Return to the beginning if the video goes back more than 5 minutes

Discussion in 'Audio & Video' started by lsk111919, Aug 24, 2020.

  1. lsk111919

    lsk111919

    Joined:
    Jul 8, 2020
    Posts:
    19
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Video;
    public class VideoHandler : MonoBehaviour
    {
    public RawImage mScreen = null;
    public VideoPlayer mVideoPlayer = null;
    private float timecount;
    private int min;
    void Start()
    {
    if (mScreen != null && mVideoPlayer != null)
    {
    StartCoroutine(PrepareVideo());
    }
    }
    protected IEnumerator PrepareVideo()
    {
    mVideoPlayer.Prepare();
    while (!mVideoPlayer.isPrepared)
    {
    yield return new WaitForSeconds(0.5f);
    }
    mScreen.texture = mVideoPlayer.texture;
    mVideoPlayer.Pause();
    }
    public void StartVideo()
    {
    mVideoPlayer.Play();
    }
    public void StopVideo()
    {
    mVideoPlayer.Pause();
    }
    public void sk()
    {
    if (!mVideoPlayer.isPlaying)
    {
    mVideoPlayer.Play();
    }
    else
    mVideoPlayer.Pause();
    }
    }
    Here's the script.
    I want to go back to the beginning when the video is played for more than 5 minutes. What should I do?
    And I am sorry if I used the wrong word or grammar because I am not good at English.
     
  2. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Hi @lsk111919

    I think you can do this with an Update() method that looks at the current time + playback status of the video.
    Also add a timestamp to your behaviour so that you can keep track of when video playback started.
    Then if X mins have elapsed since playback started, you stop/replay or seek in the video.

    Does that sound good?
     
  3. lsk111919

    lsk111919

    Joined:
    Jul 8, 2020
    Posts:
    19
    Sorry, I don't think I understand well. I tried it once but it didn't work out. I'd appreciate it if you could explain it one more time. I haven't started UNITY for a week, so I'm very immature.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Video;
    public class VideoHandler : MonoBehaviour
    {
    public RawImage mScreen = null;
    public VideoPlayer mVideoPlayer = null;
    private float timecount;
    private int min;
    void Start()
    {
    if (mScreen != null && mVideoPlayer != null)
    {
    StartCoroutine(PrepareVideo());
    }
    }
    protected IEnumerator PrepareVideo()
    {

    mVideoPlayer.Prepare();
    while (!mVideoPlayer.isPrepared)
    {
    yield return new WaitForSeconds(0.5f);
    }
    mScreen.texture = mVideoPlayer.texture;
    mVideoPlayer.Pause();
    }

    void update()
    {
    if (mVideoPlayer.canSetTime)
    {
    mVideoPlayer.time = 300.0f;
    mVideoPlayer.Stop();
    mVideoPlayer.Play();
    }
    }
    public void sk()
    {
    if (!mVideoPlayer.isPlaying)
    {
    mVideoPlayer.Play();
    }
    else
    mVideoPlayer.Pause();
    }

    }
     
  4. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Try something like this:

    Code (CSharp):
    1.  
    2. public class TestThing : MonoBehaviour
    3. {
    4.     public VideoPlayer mVideoPlayer = null;
    5.     private DateTime timeVideoStarted;
    6.     private float numSecondsResetVideo = 5.0f; // restart after X seconds
    7.  
    8.     void Start()
    9.     {
    10.         // Assume video autostarts
    11.         timeVideoStarted = DateTime.Now;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         var currTime = DateTime.Now;
    17.         var elapsed = currTime - timeVideoStarted;
    18.         // If X seconds have elapsed, reset
    19.         if (elapsed.TotalSeconds > numSecondsResetVideo)
    20.         {
    21.             mVideoPlayer.time = 0.0f; // seek to start
    22.             timeVideoStarted = DateTime.Now;
    23.         }
    24.     }
    25. }
    I added this behaviour to a 3D Plane that also has a VideoPlayer component, configured to autostart a given video file.
     
  5. lsk111919

    lsk111919

    Joined:
    Jul 8, 2020
    Posts:
    19
    Thanks to you, I solved it smoothly.
    I will study harder UNITY!!
     
    unitybru likes this.