title: How to convert Unity AudioClip into byte array suitable for Firebase Storage upload
body: <p>I am using Firebase Storage on Unity3D to store microphone recordings from the user. How do I convert an AudioClip into a byte stream that can be uploaded to Firebase Storage?</p>
  
  <p>While the project is uploading sound files at the appropriate time, the files found in Firebase are not readable by any player.</p>
  
  <p>I have made sure that:</p>
  
  <ul>
  <li>I am actually getting the proper uncorrupted recordings that I wish to upload.</li>
  <li>My upload audio clip method works for uploading files from my computer hard drive to Firebase (including the recording that I intend to upload) using PutFileAsync() rather than PutBytesAsync()</li>
  <li>My project also uploads images to the same Firebase, these images are uploaded without issue</li>
  </ul>
  
  <p>This leads me to conclude that my formatting of the byte stream is incorrect.</p>
  
  <p>My current method to convert the current recording into a byte array for upload. This code was essentially copied from (<a href="https://github.com/steelejay/LowkeySpeech/blob/master/Code/SavWav.cs" rel="nofollow noreferrer">https://github.com/steelejay/LowkeySpeech/blob/master/Code/SavWav.cs</a>), but instead of having it write the bytesData to a file, it instead sends returns the bytesData to be uploaded to Firebase.</p>
  
  <pre><code>public byte[] returnByteArrayForCurrentRecording()
      {
          var samples = new float[audioClip.samples];
  
          audioClip.GetData(samples, 0);
  
          Int16[] intData = new Int16[samples.Length];
  
          Byte[] bytesData = new Byte[samples.Length * 2];
  
          int rescaleFactor = 32767;
  
          for (int i = 0; i &lt; 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;
  
      }
  </code></pre>
  
  <p>My method that takes the byte array from the previous method and, uploads it to Firebase.</p>
  
  <pre><code>string saveAudio(DatabaseReference objectReference)
      {
          microphoneRecorder.endAudioRecording();
          byte[] audioStream = microphoneRecorder.returnByteArrayForCurrentRecording();
          StorageReference objectAudioRef= audioRef.Child(objectReference.Key.ToString()+".mp3");
          objectAudioRef.PutBytesAsync(audioStream);
          return objectAudioRef.ToString();
      }
  </code></pre>
  
  <p>I expect this to upload a playable mp3 to FirebaseStorage. However, none of the sound files uploaded using this method are readable by any media player.</p>
  
  <p>As always, thank you for taking the time.</p>