Unity 字节数组转换成音频

这个是我同事开发过程遇到的问题,被我同事解Unity 字节数组转换成音频_第1张图片
决我就复制过来分享给大家

 public static byte[] ConvertClipToBytes(AudioClip clip)
  {
        float[] samples = new float[clip.samples];
            clip.GetData(samples, 0);
        short[] intData = new short[samples.Length];
        //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
        byte[] bytesData = new byte[samples.Length * 2];
          //bytesData array is twice the size of
        //dataSource array because a float converted in Int16 is 2 bytes.
        int rescaleFactor = 32767; //to convert float to Int16
        for (int i = 0; i < samples.Length; i++)
          {
            intData[i] = (short)(samples[i] * rescaleFactor);
            byte[] byteArr = new byte[2];
            byteArr = BitConverter.GetBytes(intData[i]);
            byteArr.CopyTo(bytesData, i * 2);
            return bytesData;
            }
        }

下面这个代码块是音频转字节的代码。
如果又帮到你给我点个赞

你可能感兴趣的:(unity,unity音频处理,unity)