将先前保存的音频文件加载为 base64 字符串 [英] Loading a previously saved audio file as base64 string

查看:33
本文介绍了将先前保存的音频文件加载为 base64 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我录制并保存了一个音频文件,这可以正常工作并在应用程序中按预期播放.但是,在录制之后,我尝试加载它,以便将音频传递到 JSON 对象中,但无法打开它.

我收到以下错误:

<块引用>

无法打开文件audio.m4a",因为无法确定其内容的文本编码.

这是我打的电话:

做{让 audioData = 尝试 NSData(contentsOfFile: String(contentsOf: audioURL!))} 抓住 {打印错误}

有什么想法可以从 base64 编码的文件中加载音频数据吗?

谢谢

解决方案

您没有解释如何保存文件,所以我只能假设(根据文件扩展名),您没有将其保存为 base64 字符串.请参阅这个问题的第一个答案.检查您是否将音频文件正确保存为 base64 字符串.但你仍然可以使用其他方式.首先加载数据:

let audioData = try Data(contentsOf: audioURL!)

然后将其转换为 base64 字符串:

let encodingString = audioData.base64EncodedString()

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()

这篇关于将先前保存的音频文件加载为 base64 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆