Esempio n. 1
0
    /// <summary>
    /// 获取录制字节数据
    /// </summary>
    /// <returns></returns>
    public byte[] GetClipData()
    {
        if (m_MicroPhoneSource.clip == null)
        {
            Debug.Log("GetClipData audio.clip is null");
            return(null);
        }

        int sample = m_MicroPhoneSource.clip.frequency * Mathf.CeilToInt(m_CurrentRecordDuration);

        float[] samples = new float[sample];


        m_MicroPhoneSource.clip.GetData(samples, 0);
        byte[] ret = null;
        using (MemoryStreamExt ms = new MemoryStreamExt())
        {
            for (int i = 0; i < samples.Length; ++i)
            {
                ms.WriteFloat(samples[i]);
            }
            ret = ms.ToArray();
        }
        ret = GZipCompress.Compress(ret);
        return(ret);
    }
    /// <summary>
    /// 封装数据包
    /// </summary>
    /// <param name="data">要发送的数据</param>
    /// <returns>封装好的数据</returns>
    private byte[] MakeDatas(byte[] data, int protoCode)
    {
        int  leng = 0;
        bool isCompress;

        if (data == null)
        {
            leng       = 5;
            isCompress = false;
        }
        else
        {
            //data = EncryptUtil.NetEncrypt(data, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected);
            isCompress = data.Length >= COMPRESS_LENGTH;
            if (isCompress)
            {
                //data = GZipCompress.Compress(data);
            }
            leng = data.Length + 5;
        }
        byte[] head = new byte[4];
        head[0] = (byte)((leng & 0xFF000000) >> 24);
        head[1] = (byte)((leng & 0x00FF0000) >> 16);
        head[2] = (byte)((leng & 0x0000FF00) >> 8);
        head[3] = (byte)((leng & 0x000000FF));

        byte[] protoCodeBuffer = BitConverter.GetBytes(protoCode);
        Array.Reverse(protoCodeBuffer);

        using (MemoryStreamExt ms = new MemoryStreamExt())
        {
            ms.Write(head, 0, 4);
            ms.WriteBool(isCompress);
            ms.Write(protoCodeBuffer, 0, protoCodeBuffer.Length);
            if (data != null)
            {
                ms.Write(data, 0, data.Length);
            }
            return(ms.ToArray());
        }
        return(null);
    }