微信小程序audio组件在ios端无法播放的解决办法
作者:🍬Black Eyed Peas🍥 发布时间:2024-04-18 09:47:26
标签:小程序,audio,组件
解决方法: 给 audio 组件绑定点击事件,手动触发播放暂停方法!
代码片段:
wxml文件
<!-- 判断是语音通话,有通话记录,通话描述不包含'未接' -->
<view class="reference"
wx:if="{{itemList.activity_type === 'phone' && itemList.activity_reference_id && tool.indexOf(itemList.comment,'未接') === -1 }}">
<!-- 语音播放 -->
<van-button class="ref-btn" wx:if="{{audioResourceMaps[itemList.activity_reference_id] === undefined}}"
loading="{{itemList.activity_reference_id === currentGettingReferenceId}}" icon="play" type="info" plain
data-reference-id="{{itemList.activity_reference_id}}" bindtap="getReference">
</van-button>
<view wx:else class="audio-box">
<!-- 语音播放暂停 -->
<van-button class="ref-btn" wx:if="{{audioResourceMaps[itemList.activity_reference_id]}}"
data-reference-id="{{itemList.activity_reference_id}}"
icon="pause" type="info" plain bindtap="pauseAudio"/>
<!-- 点击没有通话录音 -->
<span wx:else class="no-audio-text">未找到通话录音</span>
</view>
</view>
wxss文件
.reference {
margin-top: 20rpx;
height: 100%;
padding: 5rpx;
box-sizing: border-box;
}
.ref-btn {
width: 80rpx;
height: 80rpx;
display: flex;
}
.ref-btn button {
width: 80rpx;
height: 80rpx;
border-radius: 50%;
}
js文件
/**
* 组件的初始数据
*/
data: {
currentGettingReferenceId: null, //正在播放的音频id
audioResourceMaps: {}, //点击过的音频列表
isPause:false, // 是否暂停
},
/**
* 组件的生命周期
*/
lifetimes: {
attached: function () {
// 因为是子组件 所以要在这里获取实例
this.audioContext = wx.createInnerAudioContext();
},
detached: function () {
// 停止播放
this.stopAudio()
// 在组件实例被从页面节点树移除时执行
},
},
methods: {
// 获取录音
getReference(e) {
let id = e.target.dataset.referenceId
if(id != this.data.currentGettingReferenceId){
this.stopAudio()
}
this.setData({
currentGettingReferenceId:id
})
// 点击获取录音url的接口。 接口请求根据自己的封装来写
WXAPI.getResourceUrl(
`/conversation/conversationsession/${id}/`, {
data_type: 'all',
}).then(({resource_url}) => {
console.log('音频地址====',resource_url,)
let url = resource_url && resource_url.indexOf('https://') > -1? encodeURI(resource_url) : null
this.data.audioResourceMaps[id] = url;
if(resource_url) this.playAudio(id,url)
this.setData({
audioResourceMaps: this.data.audioResourceMaps
})
console.log('播放过的列表=====',this.data.audioResourceMaps)
}).catch(function (e) {
console.log(e)
})
},
// 暂停
pauseAudio(){
this.setData({
isPause: !this.data.isPause
})
let id = this.data.currentGettingReferenceId
console.log(id,'播放暂停的id')
const innerAudioContext = this.audioContext
if(this.data.isPause){
innerAudioContext.pause()
console.log('暂停播放')
}else{
innerAudioContext.play()
console.log('继续播放')
}
},
// 停止播放
stopAudio(){
const innerAudioContext = this.audioContext
innerAudioContext.stop()
let obj = this.data.audioResourceMaps
for(let item in obj){
delete obj[item]
}
// 停止播放就要把播放列表id对应的音频地址做清空处理
this.setData({
audioResourceMaps: obj,
currentGettingReferenceId:null
})
console.log('停止播放')
},
// 播放
playAudio(id,url) {
const innerAudioContext = this.audioContext
console.log(url, '音频的地址')
if(url){
innerAudioContext.src = url
innerAudioContext.play()
innerAudioContext.onPlay(() => {
console.log('开始播放')
})
innerAudioContext.onTimeUpdate(() => {
console.log(innerAudioContext.duration,'总时长')
console.log(innerAudioContext.currentTime,'当前播放进度')
})
setTimeout(() => {
console.log(innerAudioContext.duration,'总时长')
console.log(innerAudioContext.currentTime,'当前播放进度')
}, 500)
innerAudioContext.onEnded(() => {
let obj = this.data.audioResourceMaps
for(let item in obj){
delete obj[item]
}
this.setData({
audioResourceMaps: obj,
currentGettingReferenceId:null
})
console.log('播放完毕')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
}
}
效果图
⚠️微信小程序中使用vant,必须要在.json 文件中引用 不然标签不会显示哦
我是在app.json文件引得 全局可用
"usingComponents": {
"van-button": "@vant/weapp/button/index",
"van-icon": "@vant/weapp/icon/index",
}
官网文档:developers.weixin.qq.com/miniprogram…
来源:https://juejin.cn/post/6979873540526833701


猜你喜欢
- 怎样才能将在表A取得的数据插入另一个表B中?(1)对于表A和表B两个表结构完全相同的话〔字段个数,相应字段的类型等等〕,可以使用 inser
- Python内建了map()和reduce()函数。如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified
- 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性。先定义cl
- 1-删除模型变量del model_define2-清空CUDA cachetorch.cuda.empty_cache()3-步骤2(异步
- 本文实例讲述了Python创建系统目录的方法。分享给大家供大家参考。具体如下:Python2 mkdir在没有上级目录时创建会失败.该方法可
- txt文件转换为XML很多目标检测的模型都是默认需要VOC的文件输入格式手上数据label是txt文件。为了避免不必要的bug,还是选择转换
- 一、卷积神经网络Yann LeCun 和Yoshua Bengio在1995年引入了卷积神经网络,也称为卷积网络或CNN。CNN是一种特殊的
- 本文研究的主要是Python编程通过pandas将数据分割成时间跨度相等的数据块的相关内容,具体如下。先上数据,有如下dataframe格式
- 最近,在搞这个东西,网上也找不到asp相关的,如果有人很早弄出来了,也不要笑话偶;费了好久,总算搞定;原理:用stream对象预读文件的头两
- 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失。因此,如果你想要编写一些更大的程序,为准备解释器输入使用一
- 前言在翻Golang官方库的过程中,发现一个有趣的库golang.org/x/time ,里面只有一个类rate,研究了一下发现它是一个限流
- 前言本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详
- model:class Profile(models.Model): user = models.OneToOneField(User, o
- js中用import导入模块和用require导入模块的区别JavaScript中,模块是一种可重用的代码块,它将一些代码打包成一个单独的单
- 作为主题的制作者, 除了实现功能, 展示界面, 还有责任使主题灵活多变, 以满足更多人不同的需求.可能一些朋友曾为选用双栏主题 (单侧边栏)
- 进程概述? 进程(Process)是计算机中已运行程序的实体。进程与程序不同,程序本身只是指令、数据及器组织形式的描述,进程才是程序(那些指
- 续上一篇文章:vue2.0 开发实践总结之入门篇 ,如果没有看过的可以移步看一下。 本篇文章目录如下:1. vue 组
- 注意:列转行的方法可能是我独创的了,呵呵,因为在网上找不到哦,全部是我自己写的,用到了系统的SysColumns(一)行转列的方法先说说行转
- 我的代码的哪些部分运行时间最长、内存最多?我怎样才能找到需要改进的地方?”在开发过程中,我很确定我们大多数人都会想知道这
- 某些杀毒软件会把正常的asp文件误认为是asp木马文件,而自动删除,影响正常使用。下面方法可能会有效避免被杀毒软件删除把dim t