Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved How to go back with Unity VideoPlayer and a url source?

Discussion in 'Scripting' started by Olotse, Jul 13, 2020.

  1. Olotse

    Olotse

    Joined:
    Jun 6, 2017
    Posts:
    10
    Hello,

    I'm trying to play a video from a url like this: https://mywebsite.com/videos?id=a2ef5ab64...
    I can play it and use a slider to move forward in the video.

    However, I can't go back. The playback time of the VideoPlayer remains unchanged.
    I would like to know if this is a normal behavior because I am using a url or if it is a problem related to the VideoPlayer?
    The VideoPlayer seems to have only a way to go forward in the playback (StepForward method and canStep property to know if we can go forward) which gives me the impression that we can't go backward, is it really impossible?

    I'm currently using Unity 2019.4.2f1, I've been looking for answers for several days but I haven't found anything about the use of urls. If you have links to one or more topics that deal with the same problem, I'm interested in.

    Finally, I ask my last question, is there a way to go backwards in the reading of a video by url?

    I'm attaching the source code that I use to launch the video and to move forward/backward with the slider.

    Have a nice day,
    Antonin

    Source code to load the video:
    Code (CSharp):
    1.     public class UIVideoPlayerContainer : UIVideoContainer
    2.     {
    3.         public VideoPlayer videoPlayer;
    4.         public AudioSource audioSource;
    5.  
    6.         public GameObject videoWaiting;
    7.         public VideoNavBar videoPlayerBar;
    8.  
    9.         float targetTime = 0;
    10.        
    11.         private void Start()
    12.         {
    13.             videoPlayer.source = VideoSource.Url;
    14.  
    15.             videoPlayer.prepareCompleted += delegate
    16.             {
    17.                 videoWaiting.SetActive(false);
    18.                 videoPlayerBar.Enabled = true;
    19.  
    20.                 videoPlayer.Play();
    21.                 videoPlayer.time = targetTime;
    22.             };
    23.         }
    24.  
    25.         public override IEnumerator PlayUrl(string url, float startTime = 0)
    26.         {
    27.             if (!url.Contains("https://") || url == "")
    28.             {
    29.                 Debug.LogError("Bad video url:" + url);
    30.                 yield break;
    31.             }
    32.  
    33.             videoPlayerBar.Enabled = false;
    34.             videoWaiting.SetActive(true);
    35.  
    36.             if (videoPlayer.isPlaying || videoPlayer.isPaused)
    37.             {
    38.                 videoPlayer.Stop();
    39.                 videoPlayer.url = "";
    40.             }
    41.  
    42.             videoPlayer.url = url;
    43.             videoPlayer.SetTargetAudioSource(0, audioSource);
    44.  
    45.             targetTime = startTime;
    46.             videoPlayer.Prepare();
    47.         }
    48.        
    49.         public override IEnumerator Reset()
    50.         {
    51.             int step = 0;
    52.  
    53.             while(step < 2)
    54.             {
    55.                 if (step == 0)
    56.                     videoPlayer.Stop();
    57.                 else if (step == 1)
    58.                     base.Reset();
    59.  
    60.                 step++;
    61.                
    62.                 yield return null;
    63.             }
    64.         }
    65.     }

    Source code attached to the slider:
    Code (CSharp):
    1.     [Serializable]
    2.     public class NavigationSliderEvent : UnityEvent<float>
    3.     { }
    4.  
    5.     [RequireComponent(typeof(Slider))]
    6.     public class NavigationSlider : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    7.     {
    8.         public VideoPlayer videoPlayer;
    9.  
    10.         public Slider slider;
    11.         public NavigationSliderEvent OnSlideFinished;
    12.  
    13.         public bool slide = false;
    14.  
    15.         private void Start()
    16.         {
    17.             slider = GetComponent<Slider>();
    18.         }
    19.  
    20.         private void Update()
    21.         {
    22.             if(!slide)
    23.                 slider.value = (float)videoPlayer.frame / videoPlayer.frameCount;
    24.         }
    25.  
    26.         public void SetTime(float value)
    27.         {
    28.             videoPlayer.frame = (long)(value * videoPlayer.frameCount);
    29.         }
    30.  
    31.         public void OnPointerDown(PointerEventData eventData)
    32.         {
    33.             slide = true;
    34.             videoPlayer.Pause();
    35.         }
    36.  
    37.         public void OnPointerUp(PointerEventData eventData)
    38.         {
    39.             OnSlideFinished?.Invoke(slider.value);
    40.             slide = false;
    41.         }
    42.     }
    I've tried with videoPlayer.time and videoPlayer.frame but neither of them want to go backwards.
    SetTime is called using OnSlideFinished in the Unity editor.
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,886
    You need to actually reset the time etc on the video player in order to play again. Just call stop:

    https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.Stop.html

    I just tested this and it resets the time so if this does not work, you are doing something incorrect somewhere that is setting the time or keeping the time of the video player

    If that doesnt work, literally enable and disable the video player object entirely. If that still does not work, instantiate a new one.
     
  3. Olotse

    Olotse

    Joined:
    Jun 6, 2017
    Posts:
    10
    I also tried something with Stop but the video always restarted to the begining. I understood that was because targetTime was fixed to 0 every time and the delegate on prepareCompleted was called when I play the video.
    Finally, I just move the SetTime method into UIVideoPlayerContainer class to be able to fix targetTime with the value.

    Thanks for your help! It allowed me to see where the error was in my reasoning.
    Antonin


    Here's my source code if anyone else is interested:
    Code (CSharp):
    1.     public class UIVideoPlayerContainer : UIVideoContainer
    2.     {
    3.         public VideoPlayer videoPlayer;
    4.         public AudioSource audioSource;
    5.  
    6.         public GameObject videoWaiting;
    7.         public VideoNavBar videoPlayerBar;
    8.  
    9.         float targetTime = 0;
    10.      
    11.         private void Start()
    12.         {
    13.             videoPlayer.source = VideoSource.Url;
    14.  
    15.             videoPlayer.prepareCompleted += delegate
    16.             {
    17.                 videoWaiting.SetActive(false);
    18.  
    19.                 videoPlayerBar.Enabled = true;
    20.                 videoPlayerBar.SetNavigationSlider(targetTime, (float)videoPlayer.length);
    21.  
    22.                 videoPlayer.Play();
    23.                 videoPlayer.time = targetTime;
    24.             };
    25.         }
    26.  
    27.         public override IEnumerator PlayUrl(string url, float startTime = 0)
    28.         {
    29.             if (!url.Contains("https://") || url == "")
    30.             {
    31.                 Debug.LogError("Bad video url:" + url);
    32.                 yield break;
    33.             }
    34.  
    35.             videoPlayerBar.Enabled = false;
    36.             videoWaiting.SetActive(true);
    37.  
    38.             if (videoPlayer.isPlaying || videoPlayer.isPaused)
    39.             {
    40.                 videoPlayer.Stop();
    41.                 videoPlayer.url = "";
    42.             }
    43.  
    44.             videoPlayer.url = url;
    45.             videoPlayer.SetTargetAudioSource(0, audioSource);
    46.  
    47.             targetTime = startTime;
    48.             videoPlayer.Prepare();
    49.         }
    50.  
    51.         public void SetTime(float value)
    52.         {
    53.             videoPlayer.Stop();
    54.  
    55.             targetTime = value;
    56.             videoPlayer.Prepare();
    57.         }
    58.  
    59.         public override IEnumerator Reset()
    60.         {
    61.             int step = 0;
    62.  
    63.             while(step < 2)
    64.             {
    65.                 if (step == 0)
    66.                     videoPlayer.Stop();
    67.                 else if (step == 1)
    68.                     base.Reset();
    69.  
    70.                 step++;
    71.              
    72.                 yield return null;
    73.             }
    74.         }
    75.     }
    I use time and set the Slider max value with video length.
    The SetTime method is called using OnSlideFinished of the NavigationSlider class and set in the Unity editor.
     
    Last edited: Jul 13, 2020