using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using System.Text;
using System;
///
/// 多媒体同步,适用于图片,ppt,视频集成在一个材质上
///
public class FunctionSync_Media : MonoBehaviour
{
public static Dictionary functionSync_MediaDic = new Dictionary();
///
/// type,编号,如果为ppt,索引
///
public string Id;
private VideoPlayer videoPlayer;
private Renderer renderer;
///
/// 关闭的texture
///
private Texture nullTexture;
///
/// 当前视频播放时间
///
[HideInInspector]
public double PlayTime;
private int currentTextureNum;
private int currentPPTnum;
[HideInInspector]
public int currentPPTsubNum;
private int currentVideoNum;
[HideInInspector]
public byte videoState;
///
/// id流
///
private byte[] idbytes;
public void Init()
{
renderer = GetComponent();
videoPlayer = GetComponent();
nullTexture = renderer.material.mainTexture;
Id = "media_" + gameObject.name;
idbytes = Encoding.UTF8.GetBytes(Id);
functionSync_MediaDic.Add(Id, this);
}
float time = 0;
private void Update()
{
//学生回拉,老师不在视频不播放
if (!GameManage.Instance.is单机模式)
{
//if (!LoadManage.Instance.loadPackage.isOwner)
//{
// if (videoPlayer.isPlaying && videoPlayer.time > PlayTime + 1)
// {
// videoPlayer.time = PlayTime + 1;
// }
//}
//else
{
if (videoPlayer.isPlaying && videoState==1)
{
time += Time.deltaTime;
if (time > 1)
{
time = 0;
PlayTime = videoPlayer.time;
//发送时间
SetVideo(currentVideoNum, 1, PlayTime);
}
}
}
}
}
public void SetTexture(int num)
{
List tmp = new List();
//id
tmp.AddRange(BitConverter.GetBytes(idbytes.Length));
tmp.AddRange(idbytes);
//图片
tmp.Add(0);
tmp.AddRange(BitConverter.GetBytes(num));
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10010, tmp.ToArray());
}
public void SetPPT(int num,int index)
{
List tmp = new List();
//id
tmp.AddRange(BitConverter.GetBytes(idbytes.Length));
tmp.AddRange(idbytes);
//ppt
tmp.Add(2);
tmp.AddRange(BitConverter.GetBytes(num));
tmp.AddRange(BitConverter.GetBytes(index));
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10010, tmp.ToArray());
}
public void SetVideo(int num,byte isOpen,double playtime)
{
List tmp = new List();
//id
tmp.AddRange(BitConverter.GetBytes(idbytes.Length));
tmp.AddRange(idbytes);
//视频
tmp.Add(1);
tmp.AddRange(BitConverter.GetBytes(num));
tmp.Add(isOpen);
tmp.AddRange(BitConverter.GetBytes(playtime));
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10010, tmp.ToArray());
}
public void CallBack(byte[] data,bool isJoinRoom=false)
{
int num=BitConverter.ToInt32(data,1+4+idbytes.Length);
if (data[4 + idbytes.Length] == 0)
{
currentTextureNum = num;
//type,num
//图片
videoPlayer.Stop();
//下载
//StartCoroutine(GetTexture(MediaPanel.instance.dicPicture[num].url, (a,tex) =>
//{
// if (a)
// {
// renderer.material.mainTexture = tex;
// }
// else
// {
// Debug.LogError("下载图片失败:" + num);
// }
//}));
}
else if(data[4 + idbytes.Length] == 1)
{
currentVideoNum = num;
videoState = data[5+4+idbytes.Length];
//if (videoPlayer.url != MediaPanel.instance.dicVideo[num].url)
//{
// videoPlayer.url = MediaPanel.instance.dicVideo[num].url;
//}
//type,num,开关,播放时间
//视频
if (data[5 + 4 + idbytes.Length] ==0)
{
//关闭
videoPlayer.Stop();
PlayTime = 0;
}
else if(data[5 + 4 + idbytes.Length] ==1)
{
//开启
videoPlayer.playbackSpeed = 1;
if (!videoPlayer.isPlaying)
{
videoPlayer.Play();
}
PlayTime = videoPlayer.time;
}
else if(data[5 + 4 + idbytes.Length] ==2)
{
//暂停
videoPlayer.playbackSpeed = 0;
PlayTime = videoPlayer.time;
}
if (isJoinRoom)
{
videoPlayer.time = BitConverter.ToDouble(data, 6 + 4 + idbytes.Length);
}
else
{
//if (!LoadManage.Instance.loadPackage.isOwner)
//{
// videoPlayer.time = BitConverter.ToDouble(data, 6 + 4 + idbytes.Length);
//}
}
}
else if(data[4 + idbytes.Length] == 2)
{
currentPPTnum = num;
//type,num,索引
//PPT
int index = BitConverter.ToInt32(data,5 + 4 + idbytes.Length);
currentPPTsubNum = index;
videoPlayer.Stop();
if (index < 0)
{
//关闭ppt
renderer.material.mainTexture = nullTexture;
}
else
{
//无索引
//下载
//StartCoroutine(GetTexture(MediaPanel.instance.dicPPT[num].subTextures[index], (a, tex) =>
//{
// if (a)
// {
// //pptTexDic[num].ppttextureDic.Add(index, tex);
// renderer.material.mainTexture = tex;
// }
// else
// {
// Debug.LogError("下载PPT图片失败:" + num);
// }
//}));
}
}
}
///
/// 下载图片
///
///
IEnumerator GetTexture(string url1, Action callback)
{
WWW www1 = new WWW(url1);
yield return www1;
if (www1.isDone)
{
if (!string.IsNullOrEmpty(www1.error))
{
Debug.LogError(www1.error);
MessagePanel.ShowMessage("获取图片失败:" + www1.error, GameObject.Find("Canvas").transform, a => { });
callback.Invoke(false,null);
}
else
{
Debug.Log("下载图片成功");
callback.Invoke(true,www1.texture);
}
}
}
}
///
/// ppt信息
///
public class PPTtextureDate
{
public Dictionary ppttextureDic = new Dictionary();
}