【Unity】通过UDP实现VideoPlayer视频同步播放

对于传输方面,本人也是菜鸟一枚,因需求而临时写以下功能代码:

 

注意:UDP传输的内容,仅提供了传输的数据。 实际应用的情况下,必须确定传输内容的校验。

代码贴出来是进行修改过的,如果需要用到该代码。

1.需要同步多个视频的功能,请更改视频播放部分(videoPlayer的视频切换以及视频的索引值)。

2.仅同步一个视频,请把视频索引部分的代码去掉,便可同步视频的播放进度。

3.仅适合网络环境较好的情况,并且会因为各种因素,造成播放出现1秒以内的不同步

 

服务端(负责发送当前视频播放进度)代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.Video;

public class ServerByUDP : MonoBehaviour
{
    public string ip = "127.0.0.1";                 //发送到目标Ip
    public int port = 8001;
    private UdpClient client;
    public float time = 0;

    //          请根据自行编写的播放脚本            //
    public VideoPlayer videoPlayer;                 //播放视频组件
    public int index = 0;                           //播放视频的索引
    //          ------------------------            //
    
    // Start is called before the first frame update
    void Start()
    {
        client = new UdpClient();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        SendMessagesToClient();
    }


    private void SendMessagesToClient()
    {
        if (time > 0.1f)
        {
            byte temp1 = 255;                                           //标识符
            byte videoIndex = (byte)index;                              //读取当前视频索引值
            ushort byFrame = (ushort)videoPlayer.frame;                 //读取当前视频播放到的帧数
            byte[] frame = new byte[2];
            frame[0] = (byte)byFrame;
            frame[1] = (byte)(byFrame >> 8);
            byte[] bytes = new byte[4] { temp1 , videoIndex, frame[1], frame[0] };
            client.Send(bytes, bytes.Length, new IPEndPoint(IPAddress.Parse(ip), port));
            time = 0;
        }
        time += Time.deltaTime;
    }
    void UDPClose()
    {
        if (client != null)
            client.Close();

    }
    void OnApplicationQuit()
    {
        UDPClose();
    }

}

 

客户端(负责接收同步信息):

using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
using UnityEngine.Video;

public class ClientByUDP : MonoBehaviour
{
    public string ip = "127.0.0.1";
    public int port = 8001;

    private UdpClient server;
    Thread connectThread;
    IPEndPoint point;

    public bool isServerActive = false;

    private VideoPlayer videoPlayer;
    private int lastIndex;
    private int lastFrame;
    private int newIndex;       
    private int newFrame;
    // Start is called before the first frame update
    void Start()
    {
        connectThread = new Thread(UDPReceive);
        connectThread.Name = "Connect Thread";
        server = new UdpClient(new IPEndPoint(IPAddress.Parse(ip), port));
        point = new IPEndPoint(IPAddress.Any, 0);
        connectThread.Start();
    }


    //视频播放理应和UDP接收端分开编写,为了给读者阅读,这里放在这里了
    //注意Unity中的组件,无法在线程中使用!
    private void Update()
    {
        if(lastIndex!=newIndex && lastFrame != newFrame)
        {
            lastIndex = newIndex;
            //切换视频的逻辑{   }
            //videoPlayer.url  = 
            videoPlayer.frame = newFrame;
            videoPlayer.Play();
        }
    }

    byte[] receiveData;
    public void UDPReceive()
    {
        while (isServerActive)
        {
            receiveData = new byte[4];
            try
            {
                receiveData = server.Receive(ref point);
                ushort frame = (ushort)((receiveData[2] << (1 * 8)) + (receiveData[3] << (0 * 8)));
                newFrame = frame;
            }
            catch
            {

            }
        }
    }

    public void UDPClose()
    {
        if (server != null)
            server.Close();
    }
    private void OnApplicationQuit()
    {
        UDPClose();
    }

}

 

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值