I record and save an audio file, this works and plays in the app as expected. After the recording, however, I try to load it so I can pass the audio into a JSON object, but can't open it.
I get the following error:
The file “audio.m4a” couldn’t be opened because the text encoding of its contents can’t be determined.
This is call I'm making:
do {
let audioData = try NSData(contentsOfFile: String(contentsOf: audioURL!))
} catch {
print error
}
Any ideas how I can load the audio data as from the file in base64 encoding?
Thanks
You didn't explain how do you save your file, so i can only assume (according to file extension), that you are not saving it as base64 string. Please refer to first answer of this question. To check if you save audio file as base64 string correctly. But still you can use other way. At first load data:
let audioData = try Data(contentsOf: audioURL!)
Then convert it to base64 string:
let encodedString = audioData.base64EncodedString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With