Unity 字节数组转换成音频

这个是我同事开发过程遇到的问题,被我同事解在这里插入图片描述
决我就复制过来分享给大家

 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;
            }
        }

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

猜你喜欢

转载自blog.csdn.net/qq_43687127/article/details/108466653