How to convert MP3 string into audioClip?

I am using the Google Text to speech API to retrieve an mp3 file. and I have received a string that looks like this:

//NExAAPwC1QAUMYAH+IiE4AIAYPh8RjgwXPg+oEHFHLB+D8QAgUOfLh8uf/lIf/4gO…

How would I go about converting this into an audioclip?

1 Like

Looking for this?

No, becuase I receive a json object, So I use the JsonUtility class to turn it into a C# Object. From the C# object is were I have access to the mp3 string AudioContent JsonData = JsonUtility.FromJson<AudioContent>(request.downloadHandler.text);

What you posted above doesn’t look like JSON to me. I’m talking about this:

This is exactly what I received back from the API. I took the string out of audioContent

{
  "audioContent": "//NExAAPwC1QAUMYAH+IiE4AIAYPh8RjgwXPg+oEHFHLB+D8QAgUOfLh8uf/lIf/4gOAhB8Pl3/EAYKOD6nAhLh9eXeD8EPEDgQWH92A5DABQT2CxcA4huoiIuP6AVAA//NExBQWmyY0AZRoAN.....",
  "timepoints": [],
  "audioConfig": {
    "audioEncoding": "LINEAR16",
    "speakingRate": 1,
    "pitch": 0,
    "volumeGainDb": 0,
    "sampleRateHertz": 24000,
    "effectsProfileId": []
  }
}

So I don’t think it’s possible to use the UnityWebRequestMultimedia.GetAudioClip

You have to figure out what the string actually contains (read the documentation from the API) and convert it to a proper byte array.

From there you can take the quick & dirty easy solution and store the bytes in a tmp file and then load mp3 clip with the GetAudioClip method by passing the tmp file path or you dig deeper into the Unity documentation and look for a methd which converts a byte array to a audio clip (Hint the first version will be easier, so I would just do that unless you notice and profile any actual performance issue with the tmp file writting / loading)

The string looks like Base64, so Convert.FromBase64String(String) Method (System) | Microsoft Learn could to the job

Thank you so much. That was exactly what I needed.

It looks potentially like it MIGHT be a b64 encoded data.

If it is (you’ll have to confirm, or test), you can convert to and from with these methods:
System.Convert.ToBase64String

System.Convert.FromBase64String

But now all you have is a byte array with mp3 data… from there you have to get the AudioClip. Meeting time for me though, tootles.

1 Like

Hi, how did you convert that audioContent string to mp3 format?

2 Likes