Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Video Seek with new Video Player (5.6)

Discussion in 'Audio & Video' started by Vern_Shurtz, May 4, 2017.

  1. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    Hello all,

    I am trying to create a simple control script so I can have basic video player control using Playmaker. I trying to figure out how to code the "Seek" function to skip to a designated frame while still playing the video. Here is my code thus far.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class VideoPlayerControlMod : MonoBehaviour {
    7.  
    8.     public UnityEngine.Video.VideoPlayer vPlayer;
    9.  
    10.     public long longFrame;
    11.     public int currentFrame;
    12.     public int currentSeek;
    13.     public long longSeek;
    14.    
    15.     void Update () {
    16.  
    17.         longFrame = (vPlayer.frame);
    18.         currentFrame = (int)longFrame;
    19.  
    20.         Debug.Log("frameCount " + vPlayer.frame);
    21.  
    22.     }
    23.  
    24.     void OnPlay()
    25.     {
    26.         vPlayer.Play();
    27.     }
    28.  
    29.     void OnPause()
    30.     {
    31.         vPlayer.Pause();
    32.     }
    33.  
    34.     void OnStop()
    35.     {
    36.         vPlayer.Stop();
    37.     }
    38.  
    39.     void OnSeek()
    40.     {
    41.         longSeek = (long)currentSeek;
    42.         vPlayer.frame += (longSeek);
    43.     }
    44. }
    Everything is working except seeking. Any help would be much appreciated.

    :)
     
  2. JermyAker

    JermyAker

    Joined:
    Aug 4, 2014
    Posts:
    17
    Hi Vern. You're close. I'd do something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Video;
    3.  
    4. [RequireComponent( typeof( VideoPlayer ) )]
    5. public class VideoPlayerControlMod : MonoBehaviour
    6. {
    7.     private VideoPlayer vPlayer;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         vPlayer = GetComponent<VideoPlayer>();
    13.     }
    14.  
    15.     public void Play()
    16.     {
    17.         vPlayer.Play();
    18.     }
    19.  
    20.     public void Pause()
    21.     {
    22.         vPlayer.Pause();
    23.     }
    24.  
    25.     public void Stop()
    26.     {
    27.         vPlayer.Stop();
    28.     }
    29.  
    30.     public void Seek( int frame )
    31.     {
    32.         vPlayer.frame = frame;
    33.     }
    34. }
     
  3. ThalesUnity

    ThalesUnity

    Joined:
    May 9, 2017
    Posts:
    3
    Hi there !

    I'm also trying to use the video Player and has a similar problem with the seek function.
    Here is mine, triggerred by a slider :

    Code (CSharp):
    1. public void Seek(float nTime)
    2.         {
    3.             if (!IsPrepared)
    4.             {
    5.                 Debug.Log("Video Not Prepared.");
    6.                 return;
    7.             }
    8.  
    9.             nTime = Mathf.Clamp(nTime, 0, 1);
    10.             vid.time = nTime * Duration;
    11.             Debug.Log("Seeking.");
    12.         }
    It put the video into a material override just working fine. But when I seek in the video with the slider, I get "Seeking" in the debug log but almost 4/5 times, the video freezes... Looks like a refresh issue... any help ?

    Edit : looks like the first seeking works well. But till the second one, the video image freezes, even if the video keeps playing (I see it with another slider with its value = video.Time).
     
    Last edited: Jun 9, 2017
  4. Bandit-Development

    Bandit-Development

    Joined:
    Dec 8, 2014
    Posts:
    7
    @ThalesUnity What platform are you trying to use seeking on? I ran into problems on Mac, iOS, and Android. However, I got seeking working very well in WIndows (I have a powerful machine, so maybe that helps).

    Here is my code if you want a look:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5. using UnityEngine.UI;
    6.  
    7. public class VideoPlayerControl : MonoBehaviour {
    8.  
    9.     [Header("Settings")]
    10.     [Range(0.1f, 10f)]
    11.     public float playbackSpeed = 1f;
    12.  
    13.     [Header("UI Controls")]
    14.     public Slider progressBar;
    15.  
    16.     //Private vars
    17.     VideoPlayer vPlayer;
    18.     public int vidFrameLength;
    19.     bool shouldStartPlaying = true;
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         vPlayer = gameObject.GetComponent<VideoPlayer>();
    24.         vPlayer.playbackSpeed = playbackSpeed;
    25.         vPlayer.Prepare();
    26.         vidFrameLength = (int)vPlayer.frameCount;
    27.         progressBar.maxValue = vidFrameLength;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update () {
    32.         if(shouldStartPlaying && vPlayer.isPrepared){
    33.             Play();
    34.             shouldStartPlaying = false;
    35.         }
    36.     }
    37.  
    38.     public void Play(){
    39.         vPlayer.Play();
    40.     }
    41.  
    42.     public void Pause(){
    43.         vPlayer.Pause();
    44.     }
    45.  
    46.     public void Stop(){
    47.         vPlayer.Stop();
    48.     }
    49.  
    50. //This is called by the slider (Event Trigger: End Drag)
    51.     public void SetFrameBySlider(){
    52.         JumpToFrame(progressBar.value);
    53.     }
    54.  
    55.     public void JumpToFrame(float frame){
    56.         vPlayer.frame = (long)frame;
    57.     }
    58. }
     
    ekh4rt likes this.
  5. ThalesUnity

    ThalesUnity

    Joined:
    May 9, 2017
    Posts:
    3
    I'm on Windows (7) too. I'm currently using a placeholder video for my tests. It looks like it is due to some issues in the video loading after seeking. I lowered the video speed (Playback speed) just to check, and at half speed I have almost no issue ! So that's probably a performance issue...
     
  6. Bandit-Development

    Bandit-Development

    Joined:
    Dec 8, 2014
    Posts:
    7
    OK, I'm on Windows 10 Pro, and I think that be part of the problem. Saw somewhere that Windows 7 had some issues with VideoPlayer. Also, what kind of performance spike are you seeing when you try seeking? I get about 50% CPU usage just when seeking, and I have a i7-6800k CPU. (My GPU, GTX 1080, does not take any of the load for whatever reason.) Also, are you preparing the video at Start?
     
  7. progmohamed

    progmohamed

    Joined:
    Oct 19, 2017
    Posts:
    1
    Hello All
    this is the solution
    public void seek( )
    {
    if (videoPlayer && slider && slider.value != _setVideoSeekSliderValue)
    {
    videoPlayer.time=(slider.value * Duration);
    }
    }