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

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

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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐