Android Question Base64 String to (mp3) file?

Multiverse app

Active Member
Licensed User
Longtime User
Currently, I am receiving a JSON response from an API having audio data, from which I need to play the MP3 file.

This is the code I am using to save the .MP3 file:

B4X:
    'strAudio contains the audio data
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "CloudTTS.mp3", False)
    Dim Data() As Byte=strAudio.GetBytes("UTF8")
    out.WriteBytes(Data, 0, Data.Length)
    out.Close

But the file seems to be corrupt.
Attached is the JSON response which is being parsed.

What is the correct way to do this?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim json As String =$"{
  "audioContent": "//NExAASSC4AAAhGAGAAEb9EED4PlHKOCc+oMEIODQfygIf3fLh+UOT+JxxzLh8RvKHFhgTwQcJz6ygfE4f6gQg/E4PgQYCEHy4fBAIh7E5QHwfPiceqDPCEMNaAnSwd//NExAkQYC4MABBGACcShoiHaCsFRmIhKGv0fJcSiXxFrOlnkcJlXKCp2DQsDU74iHgsEgqCpG0qCowNFjwKgiWqDvEpCj+GIcZiwVuEhqwIrxAIHkCB4fBMEYPigwMX//NExBoQYDoEAH4MBF4otRwEF85/gh6eEAgoEwQda7KKOVU2lLsPifDCMVbWmEX2OIFzeXowKwPzA2ADMn1PE1aTDzBUCaMGMAcwnAfB0B8wCALB6bps4nIy1uHqGX2a//NExCsbmI4MAPYGTZUwEIaZC3cwMfAo5Ed05yPApYGkeJ8vyZX/18piu5X9tA1t3ee43Q8le2BLrjKNPKh0Pv/2g2s9tT3a5nuxYjmGole1RJLLiJWnUUuJLsaHhpQ///NExA8UqV5QAOIKlAOWhiMCmGwmno3N9njnqiad7StNPRBRPLHg0UJAWSWwfwgo30AAUEGOMFKz1Ofn/U56i4MID4YKcTv/+p3rkOicyfy6vTiqpkQsRGaGgIGDApE6//NExA8U+XKEANvQlFRxUlMPA0IkrILft/e/pSmf//r/f9JJ3jGn1MsKQbgmCXT7mXFOyC59kg3c8JAgssXe6////4S0SKQXcPixyqr//9P/0cMeKrGKgDil3hGBAyMO//NExA4VoW6YANvSlGmozICYS/KTLJ4+qTa19Xz///95rakeKgi5m8GoLsW0YZezICk0GSnPTAgRhIDAi0G1yDNy/X//v/1m7sNUpouGQfcLP6v//rX/+nXKDmD60xgw//NExAoT4YKkANNalch4caKNFmrSKmGGLHqWh6fp9E6fRKyWBTgAUCTCGKIfjFiMYmp+iYlAli4bCNBygsRtMSaiYaC6/V6m0UUnSrPoxqYnr9X/3ksAVS7rzDjNKNTu//NExA0TKRakANHecAUzEQkaeEr8avWY3VPn5Xo75OsBzAfULV7grlvXzqLXFoLW0NbYh0clMFQM04VZ2aLTxUFTSyqmnTq0f/o//1LVm3GkGDlcbd+pa0l5GHReiv87//NExBMUsoacAMlEufzf/po5SlIAQm6CxW/uvtEWN////9ezL7PbJnpPV6IQIALMQWHFqd4AITSRkIVyFFoANDnkV6j+dG+fiMPEXD6isKKCfX//////////////f/qr//NExBMV4xKkABASudyGeccs+nb5fV5IIsJWiiqwXRF0ckc0K1vMaUkOROBBfJrnGyM23KAboGEsm97KaKiQkVPw3F1Vkcl20cNnS8SSDDSY6v/////////////ubr+u//NExA4R6vq4AAAMufuZ37eHg5OTz3NKDoOMMNNB00yCk1hFEOVImUAnMsmgrqqTSQ1KTNAsIo+4x6Rimg1HXfX/SOEWh13iAEUXRg43////9f///////XtXS+urGQpV//NExBkR+tK8AChKuYqSIDTihxyiIuQ5ihJxdmdjFUocFzyOZGQPGxyOHTKLKxMScrV3RxkFYWbjDtKoyu0iwQqOxV9hCqeNZflvKZ/L3///9DGiIAwi88+3/eiuzf////NExCQSWeakAMnEmP9aMqEa+XoZLG2QxUFK5ITMnQEaWCtyzqzpUSnSOCpb9BG7EqUV/tK/Q0ehbeAHOYtkmatSDGH2clN1dXbUys4T0zYOgbJIGgXQljJI/W0QigoK//NExC0RaZ6cANNKlQAiyGM///tqqoIDFFxQWIFiwgJBQK2GXELLG0kQiVZRmMkjwAEsvSdC4Uwa179Ho6+ro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9Zz/5Md9FkxDcps9yiebOMUTe3XqAh0UATWxwEPGci8dKXZXXf7//NExF0SKZKUANleleu8v//3gmTKeauqFQWpwwDRfqTCpUsiQWo0VgjR723/T/P/3W12/7jRsPOzR9QJ1Znb3Wf0ykAQGIBzCog2dFMDCmSEIEPA9/mNL////3gzMS+d//NExGcRgY6QANhelc+TwnQcRfnxZNsdiU7KkF5XrLyWD9x/rP9dfOtUtrHeva4rGtp9tjHO8OhpnQCugy9tP/vDcSIxoEM5AFBYakV7GP////RAa2fsFmUaRUw3ySnm//NExHQSQY6MANielXs7XmGCf6pZ9+akeDvPrrfz/nWt43CnrLAfM17Wi/UqlTRwuzMwFBR86TY1JY5kgQnxYvBrEb38/////CX5a6k5QxCywG2N4vqdP5qVMGGwxY0f//NExH4SiZKAANhelM8v1XVs6zjFvi9cxMa1SENNvjWgFQqE4cGG81m2nh+M1SUOFAUIEBpsI6f///////PnSYZjwKIpgAkABseQEieSHMTOEolyz/3+UzcJkTUuriq0//NExIYRWXp4ANBelThIpmM3QPcpMEqOJBMcFL8upJQ63/////WqUxBSREFCgBbw/gkQMIOUL0OUc5KmyyRKIUd+V5nu6rJKSbqjCEUFBRlCSYzEGqiRraAAQwx0Od2A//NExJMPORJsANCWcR7kL/////7mSysQUZQOYJIBTh5AmoXElQupCLsuqAR4asaiLEREOFCW7////9f19rdo5NFwgag6mQTgZB9cCLlkEf3fZ9B4KHivbl0///poimCs//NExKkP4RZcANHacT8F0QwEoqCcDaaIksBZIiWI94rEVsjZ////+ne1j7+7fcMVvQ4Oi3oBAgAhP1HNERE9w4uSFo4f/rt2kiBDFIv68yAEBQKHUQCiMSMnRtzX1RGB//NExLwSeRJIANhacAPkA+UE4PioHZIf60ZfnFn/I////qX9t8mw8glhbExg8My+SBfdotWFqis0Ct2HcHHf4iyXXjKUZNC9m0lhuDgWTsJ+F+eY6DnhHyoHRwSwrJkA//NExMUQUPooAMFUcCJyApMdJSjlyecYgkJJLu/6U+7j73e6qd80ikw9B2Q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qqqqqqTEFNRTMu//NExKwAAANIAAAAADk5LjWqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqTEFNRTMu//NExKwAAANIAAAAADk5LjWqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqTEFNRTMu//NExKwAAANIAAAAADk5LjWqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//NExKwAAANIAAAAAKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//NExKwAAANIAAAAAKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
}"$
    Dim parser As JSONParser
    parser.Initialize(json)
    Dim root As Map = parser.NextObject
    Dim audioContent As String = root.Get("audioContent")

    ' You are missing to DECODE the base64 String.....
    Dim su As StringUtils ' Tested in B4J using jStringUtils / use Stringutils for B4A
    Dim decoded() As Byte = su.DecodeBase64(audioContent)
  
  
    Dim out As OutputStream = File.OpenOutput(File.DirApp, "CloudTTS.mp3", False)
    out.WriteBytes(decoded, 0, decoded.Length)
    out.Close
 
Upvote 0
Top