Advertisement
Guest User

Television.cs

a guest
Mar 14th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using UnityEngine.Video;
  2. using Mirror;
  3. using YoutubePlayer;
  4.  
  5. namespace Era.Modules.Props
  6. {
  7.     public class Television : Prop
  8.     {
  9.         public VideoPlayer Player;
  10.        
  11.         private double _StoredTime;
  12.  
  13.         public override void OnStartClient()
  14.         {
  15.             Player.prepareCompleted += PrepareCompleted;
  16.  
  17.             CmdUpdateState();
  18.         }
  19.  
  20.         public override void Interact(Player player)
  21.         {
  22.             // TODO: Open UI
  23.         }
  24.  
  25.         [ClientRpc]
  26.         private void RpcPlayFile(string file)
  27.         {
  28.             SharedPlayFile(file, 0, true);
  29.         }
  30.  
  31.         [Command(ignoreAuthority = true)]
  32.         private void CmdRequestFile(string file)
  33.         {
  34.             ServerRequestFile(file);
  35.         }
  36.  
  37.         public void ServerRequestFile(string file)
  38.         {
  39.             file = PreProcessURL(file);
  40.  
  41.             RpcPlayFile(file);
  42.             SharedPlayFile(file, 0, true);
  43.         }
  44.  
  45.         [Command(ignoreAuthority = true)]
  46.         private void CmdUpdateState(NetworkConnectionToClient conn = null)
  47.         {
  48.             RpcUpdateState(conn, Player.url, Player.time, Player.isPlaying);
  49.         }
  50.  
  51.         [TargetRpc]
  52.         private void RpcUpdateState(NetworkConnection conn, string file, double time, bool play)
  53.         {
  54.             SharedPlayFile(file, time, play);
  55.         }
  56.  
  57.         private void SharedPlayFile(string file, double time, bool play)
  58.         {
  59.             Player.url = file;
  60.             _StoredTime = time;
  61.  
  62.             if (play) {
  63.                 Player.Prepare();
  64.             }
  65.         }
  66.  
  67.         private void PrepareCompleted(object sender)
  68.         {
  69.             Player.time = _StoredTime;
  70.             Player.Play();
  71.         }
  72.  
  73.         private string PreProcessURL(string url)
  74.         {
  75.             if (url.Contains("youtube")) {
  76.                 YoutubeVideoMetaData meta = YoutubeDl.GetVideoMetaData(url, YoutubeDlOptions.Default);
  77.                 url = meta.Url;
  78.             }
  79.  
  80.             return url;
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement