Search Unity

Video Can't get Unity VideoPlayer to set time ("seek")?

Discussion in 'Audio & Video' started by tatea, Mar 19, 2018.

  1. tatea

    tatea

    Joined:
    Sep 1, 2015
    Posts:
    8
    Hi all,

    I'm trying to swap out too near identical videoclips (both the same length) at runtime, using a single VideoPlayer.

    Can anyone tell me why this code doesn't work? it's just restarting the second video from the start instead of setting it to play from the 'snapshotvideoseek' which is a snapshot of the videoplayer.time when the 'swap video' button is pressed.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Video;
    6.  
    7. public class VideoSwitcher : MonoBehaviour {
    8.  
    9.     public VideoPlayer videoPlayer;
    10.  
    11.     public VideoClip videoclip1;
    12.     public VideoClip videoclip2;
    13.     public VideoClip videoclip3;
    14.     public VideoClip videoclip4;
    15.     public VideoClip videoclip5;
    16.  
    17.     public double currentVideoSeek;
    18.     public double snapshotVideoseek;
    19.  
    20.     void Update(){
    21.         currentVideoSeek = videoPlayer.time;
    22.     }
    23.  
    24.     public void video1_seek(){
    25.         videoPlayer.Play ();
    26.     }
    27.  
    28.     public void video2_seek(){
    29.         snapshotVideoseek = currentVideoSeek;
    30.         videoPlayer.clip = videoclip2;
    31.         videoPlayer.time = snapshotVideoseek;
    32.         videoPlayer.Play ();
    33.     }
    34.      
    35. }


    So 'video2_seek' should save the snapshot of the current time, load the new video clip, set the videoplayer's time to the snapshot, then play. But the videoclip2 just begins playing from 0.

    Any help is much appreciated!
     
    christixr likes this.
  2. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    I seem to have a similar problem, and it looks like it's not possible to set `time` before calling `Play`. I haven't solved my own problem completely, so I'm not 100% sure this is the solution, but have you tried swapping the last two lines in `video2_seek`?
     
  3. shashkes

    shashkes

    Joined:
    Mar 16, 2017
    Posts:
    19
    was having the same problem.
    check
    VideoPlayer.canSetTime - video needs to be playing for this to be true
    videoPlayer.isPrepared - write videoPlayer.Prepare() and then it still takes a few frameupdates for it to be prepared. so better to set the time in a co-routine or update function that checks for these two things
     
    Last edited: Aug 9, 2018
    ShantiB95 likes this.
  4. AdamSt

    AdamSt

    Joined:
    Jun 2, 2013
    Posts:
    18
    It's really confusing and working weird. Long story short - i'm trying to create simple player for my app - and to add fast forward and rewind. When I use (video player is playing, playback speed is 1) videoPlayer.time -= someVariable - it works perfectly, rewinds my video, renders frames. When I use, in THE SAME CIRCUMSTANCES videoPlayer.time += someVariable - it only adds this variable ONE TIME, so it is not working as expected. So to fast forward video, I'm modifying in this case videoPlayer.playbackSpeed = someVariable. Really funny! Oh, p.s. When I tried to use videoPlayer.playbackSpeed = -X for rewind - it is not working :D