using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; using System.Text; using System; /// /// 视频播放同步 /// public class FunctionSync_Video : MonoBehaviour { public static Dictionary dicVideo = new Dictionary(); public string id; private VideoPlayer videoPlayer; private st_Motions st = new st_Motions { m_iOperaType = 10009 }; /// /// 视频播放器状态 /// public int State; /// /// 当前视频播放时间 /// private double PlayTime; private bool hasInit; private int index; void Start() { Init(); } public void Init() { if (!GameManage.Instance.is单机模式) { if (!hasInit) { st.area = LoadManage.Instance.currentRoomArea; id = "video_" + gameObject.name; videoPlayer = GetComponent(); byte[] idbyte = Encoding.UTF8.GetBytes(id); byte[] tmp = new byte[4 + 4 + 8 + idbyte.Length]; index = 4 + idbyte.Length; //id Array.Copy(BitConverter.GetBytes(idbyte.Length), 0, tmp, 0, 4); Array.Copy(idbyte, 0, tmp, 4, idbyte.Length); //状态 //时间 st.m_sOperaData = tmp; dicVideo.Add(id, this); hasInit = true; } } } private void Update() { //学生回拉,老师不在视频不播放 if(!GameManage.Instance.is单机模式 ) { if(videoPlayer.isPlaying && videoPlayer.time>PlayTime+1) { videoPlayer.time = PlayTime+1; } } } /// /// 操作同步视频 /// public void SetAudio(int num) { State = num; if (num == 0) { //关闭 videoPlayer.Stop(); PlayTime = 0; } else if (num == -1) { //暂停 videoPlayer.playbackSpeed=0; PlayTime = videoPlayer.time; } else if (num == -2) { //继续 videoPlayer.playbackSpeed = 1; PlayTime = videoPlayer.time; } else if(num==1) { //播放 videoPlayer.playbackSpeed = 1; videoPlayer.Play(); PlayTime = videoPlayer.time; } SendSync(); } /// /// 回调(老师不执行) /// /// /// public void CallBack(int state,double time) { State = state; PlayTime = time; if (State == 0) { //关闭 videoPlayer.Stop(); } else if (State == -1) { //暂停 videoPlayer.playbackSpeed = 0; } else if(State==1) { //播放 videoPlayer.playbackSpeed = 1; videoPlayer.Play(); } videoPlayer.time = time; } public void JoinRoomCallBack(int state, double time) { State = state; PlayTime = time; if (State == -1) { //暂停 videoPlayer.Play(); videoPlayer.playbackSpeed = 1; videoPlayer.time = time; videoPlayer.playbackSpeed = 0; } else if (State == 1) { //播放 videoPlayer.Play(); videoPlayer.playbackSpeed = 1; videoPlayer.time = time; } } /// /// 更新时间 /// /// public void SetCurrentTime(double time) { PlayTime = time; SendSync(); } private void SendSync() { //状态 Array.Copy(BitConverter.GetBytes(State), 0, st.m_sOperaData, index, 4); //时间 Array.Copy(BitConverter.GetBytes(PlayTime), 0, st.m_sOperaData, index + 4, 8); LoadManage.Instance.RSclient.Send(st); } }