uniapp对接融云IM即时通讯,语音消息无法播放

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的地址文件保存到该应用的沙盒目录中


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对远程地址进行转义

let remote = 'https://bucket7-a-3.ronghub.com/xxxx.aac?e=xxx&token=xxxx'
let path = encodeURI(remote)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雾里桃花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值