Search Unity

Video Switching clip for videoPlayer in Unity 5.6.0f3 causes Lag and makes CPU usage 100%

Discussion in 'Audio & Video' started by ViralArcade, Apr 11, 2017.

  1. ViralArcade

    ViralArcade

    Joined:
    Feb 21, 2017
    Posts:
    4
    Making project with SteamVR and unity 5.6.0f3. making a simple switching video mechanism and everytime the videoclip switches within the videoplayer and it plays the new clip. it causes a CPU usage spike which shows that the problem is with VideoPlayer.set_clip()
    cpu error.PNG
    and my steamVR switches from the application to the compositor screen for a split second.
    My settings for the videoplayer is
    videoplayersetting.PNG
    I have also tried stopping the clip before switching and playing again.
    This is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Video;
    6. [RequireComponent(typeof(AudioSource))]
    7. public class PlayVideo : MonoBehaviour
    8. {
    9.    
    10.      public VideoClip clip;
    11.      private SerialUISummoner canvas;
    12.      VideoPlayer video;
    13.      int vsyncprevious;
    14.      private bool playing = false;
    15.      // Use this for initialization
    16.      void Start()
    17.      {
    18.          video = GetComponent<VideoPlayer>();
    19.          video.clip = clip;
    20.          video.SetTargetAudioSource(0, GetComponent<AudioSource>());
    21.          canvas = Object.FindObjectOfType<SerialUISummoner>();
    22.      }
    23.      // Update is called once per frame
    24.      void Update()
    25.      {
    26.          if (canvas != null)
    27.          {
    28.              if (canvas.Showing && !playing)
    29.              {
    30.                  playing = true;
    31.                  print("START");
    32.                  PlayVid();
    33.              }
    34.              if (!canvas.Showing && playing)
    35.              {
    36.                  playing = false;
    37.                  StopVid();
    38.              }
    39.          }
    40.      }
    41.      public void PlayVid()
    42.      {
    43.          video.Play();
    44.      }
    45.      public void StopVid()
    46.      {
    47.          video.Stop();
    48.      }
    49.      public void Change()
    50.      {
    51.          if (video != null)
    52.          {
    53.              playing = false;
    54.              video.clip = clip;
    55.                  playing = true;
    56.                  PlayVid();
    57.          }
    58.      }
    59. }
     
    ALMALOCO likes this.
  2. Simon_says

    Simon_says

    Joined:
    Oct 19, 2013
    Posts:
    141
    I have same problem in 5.6.1, any solution?
     
    ALMALOCO likes this.
  3. ALMALOCO

    ALMALOCO

    Joined:
    Dec 15, 2016
    Posts:
    1
    same problem too... i want to fix this problem as soon as possible..