2

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?

While the project is uploading sound files at the appropriate time, the files found in Firebase are not readable by any player.

I have made sure that:

  • I am actually getting the proper uncorrupted recordings that I wish to upload.
  • 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()
  • My project also uploads images to the same Firebase, these images are uploaded without issue

This leads me to conclude that my formatting of the byte stream is incorrect.

My current method to convert the current recording into a byte array for upload. This code was essentially copied from (https://github.com/steelejay/LowkeySpeech/blob/master/Code/SavWav.cs), but instead of having it write the bytesData to a file, it instead sends returns the bytesData to be uploaded to Firebase.

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

    }

My method that takes the byte array from the previous method and, uploads it to Firebase.

string saveAudio(DatabaseReference objectReference)
    {
        microphoneRecorder.endAudioRecording();
        byte[] audioStream = microphoneRecorder.returnByteArrayForCurrentRecording();
        StorageReference objectAudioRef= audioRef.Child(objectReference.Key.ToString()+".mp3");
        objectAudioRef.PutBytesAsync(audioStream);
        return objectAudioRef.ToString();
    }

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.

As always, thank you for taking the time.

0 Answers0