Search Unity

Question Stop VideoPlayer(s) running in background

Discussion in 'Audio & Video' started by stipla, Mar 9, 2021.

  1. stipla

    stipla

    Joined:
    Mar 3, 2021
    Posts:
    5
    Here everyone : )

    Question from Paris in France
    Today is a really beautiful day here with a fantastic blue sky
    hope its cool in your town too

    i have multiples objects with the same script.
    i start playing a video everytime i click on one of this objects
    the problem is : when i click on a new object i want to stop all others videos playing
    i tried lot of things but my code lvl is come to an end : (

    Thank you <3

    Code (CSharp):
    1. public class PlayMovieVP : MonoBehaviour
    2. {
    3.     public UnityEngine.Video.VideoClip videoClip;
    4.  
    5.     void Start()
    6.     {
    7.         var videoPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
    8.         var audioSource = gameObject.AddComponent<AudioSource>();
    9.  
    10.         videoPlayer.playOnAwake = false;
    11.         videoPlayer.clip = videoClip;
    12.         videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.MaterialOverride;
    13.         videoPlayer.targetMaterialRenderer = GetComponent<Renderer>();
    14.         videoPlayer.targetMaterialProperty = "_MainTex";
    15.         videoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.Direct;
    16.         videoPlayer.SetTargetAudioSource(0, audioSource);
    17.     }
    18.  
    19.  
    20.     void OnMouseDown()
    21.     {
    22.  
    23.         var vp = GetComponent<UnityEngine.Video.VideoPlayer>();
    24.         if (vp.isPlaying)
    25.         {
    26.             vp.Pause();
    27.         }
    28.         else
    29.         {
    30.             vp.Play();
    31.         }
    32.     }
    33. }