uniapp对接融云IM即时通讯,语音消息无法播放
问题背景
最近使用uniapp对接融云的即时通讯sdk,发送语音消息后,本地音频(local)和远程音频(remote)都无法正常播放
播放local时,uniapp的音频api返回了{errCode: -5}
播放remote时,uniapp的音频api返回了{errCode: -99}
解决方案
1.本地音频播放
经过验证,融云返回的音频local地址是以file://开头的平台绝对路径,uniapp播放该地址时报错code:-5(有可能是权限问题,app无法读取这个路径中的数据)
解决方法:使用uni.saveFile,将local的地址文件保存到该应用的沙盒目录中
javascript
async created() {
let local = 'file:///data/user/0/xxxxxxx.aac'
let path = await this.getLocalUrl(local)
},
methods: {
getLocalUrl(local) {
let that = this;
return new Promise((reslove,reject) => {
uni.saveFile({
tempFilePath: local,
success: (res) => {
reslove(res.savedFilePath)
},
fail: (err) => {
console.log('saveFile fail = ', err);
reject(err)
}
})
})
}
}
使用以上方法成功解决本地音频无法播放的问题
【注意】uni.saveFile每次都会重新保存一个文件,需要自己处理一下,避免撑爆存储空间
2.远程音频播放
经验证,使用uniapp音频api播放remote音频时,会有一段时间没有反应,随后报错-99,感觉是remote地址中有一些特殊字符,导致音频api解析失败
解决方法:使用encodeURI对远程地址进行转义
javascript
let remote = 'https://bucket7-a-3.ronghub.com/xxxx.aac?e=xxx&token=xxxx'
let path = encodeURI(remote)