Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a previously saved audio file as base64 string

Tags:

ios

swift

swift3

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

like image 890
rubyist Avatar asked Dec 10 '22 11:12

rubyist


1 Answers

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()
like image 73
Eugene Laminskiy Avatar answered Dec 30 '22 21:12

Eugene Laminskiy