原由
同样的是因为小程序官方不维护audio组件了
音频组件的要求与限制
1、点击播放或者暂停
2、显示播放进度及总时长
3、通过图标变化显示当前音频所处状态(暂停/播放/加载中)
4、页面音频更新时刷新组件状态
5、全局有且只有一个音频处于播放状态
6、离开页面之后要自动停止播放并销毁音频实例
材料/属性/方法
让我们开始吧
uni-app Vue
- 同样的先构造
DOM结构
<view class="custom-audio">
<image v-if="audioSrc !== undefined && audioSrc !== null && audioSrc !== ''" @click="playOrStopAudio" :src="audioImg" class="audio-btn" />
<text v-else @click="tips" class="audio-btn">无音源</text>
<text>{{ fmtSecond(currentTime) }}/{{ fmtSecond(duration) }}</text></view>复制代码
- 定义接受的组件
props: { audioSrc: { type: String, default: ''
},
},复制代码
- 定义
CustomAudio组件的初始化相关的操作,并给innerAudioContext的回调添加一些行为(之前Taro那篇我们踩过的坑这里就直接上代码了)
import { formatSecondToHHmmss, afterAudioPlay, beforeAudioRecordOrPlay } from '../../lib/Utils'const iconPaused = '../../static/images/icon_paused.png'const iconPlaying = '../../static/images/icon_playing.png'const iconStop = '../../static/images/icon_stop.png'const iconLoading = '../../static/images/icon_loading.gif'// ...data() { return { audioCtx: null, // 音频上下文
duration: 0, // 音频总时长
currentTime: 0, // 音频当前播放的时长
audioImg: iconLoading, // 默认状态为加载中
}
},watch: { audioSrc: {
handler(newSrc, oldSrc) { console.log('watch', newSrc, oldSrc) this.audioImg = iconLoading this.currentTime = 0
this.duration = 0
if (this.audioCtx === undefined) { this.audioCtx = uni.createInnerAudioContext() this.onTimeUpdate = this.audioCtx.onTimeUpdate this.bindAuidoCallback(this.audioCtx)
} else { this.audioCtx.src = newSrc
} if (this.audioCtx.play) { this.audioCtx.stop()
getApp().globalData.audioPlaying = false
}
}
}
},
mounted() { this.audioCtx = uni.createInnerAudioContext() this.audioCtx.src = this.audioSrc this.audioCtx.startTime = 0
this.bindAuidoCallback(this.audioCtx)
},methods: {
bindAuidoCallback(ctx) {
ctx.onTimeUpdate((e) => { this.onTimeUpdate(e)
})
ctx.onCanplay((e) => { this.onCanplay(e)
})
ctx.onWaiting((e) => { this.onWaiting(e)
})
ctx.onPlay((e) => { this.onPlay(e)
})
ctx.onPause((e) => { this.onPause(e)
})
ctx.onEnded((e) => { this.onEnded(e)
})
ctx.onError((e) => { this.onError(e)
})
},
tips(){
uni.showToast({ title: '无效音源,请先录音', icon: 'none'
})
},
playOrStopAudio() { if (this.audioCtx === null) { this.audioCtx = uni.createInnerAudioContext() this.audioCtx.src = this.audioSrc this.bindAuidoCallback(this.audioCtx)
} if (this.audioCtx.paused) { if (beforeAudioRecordOrPlay('play')) { this.audioCtx.play() this.audioImg = iconPlaying
}
} else { this.audioCtx.pause()
afterAudioPlay() this.audioImg = iconPaused
}
},
onTimeUpdate(e) { console.log('onTimeUpdate', this.audioCtx.duration, this.audioCtx.currentTime) if (this.audioCtx.currentTime > 0 && this.audioCtx.currentTime <= 1) { this.currentTime = 1
} else if (this.currentTime !== Math.floor(this.audioCtx.currentTime)) { this.currentTime = Math.floor(this.audioCtx.currentTime)
} const duration = Math.floor(this.audioCtx.duration) if (this.duration !== duration) { this.duration = duration
}
},
onCanplay(e) { if (this.audioImg === iconLoading) { this.audioImg = iconPaused
} console.log('onCanplay', e)
},
onWaiting(e) { if (this.audioImg !== iconLoading) { this.audioImg = iconLoading
}
},
onPlay(e) { console.log('onPlay', e, this.audioCtx.duration) this.audioImg = iconPlaying if (this.audioCtx.duration > 0 && this.audioCtx.duration <= 1) { this.duration = 1
} else { this.duration = Math.floor(this.audioCtx.duration)
}
},
onPause(e) { console.log('onPause', e) this.audioImg = iconPaused
},
onEnded(e) { console.log('onEnded', e) if (this.audioImg !== iconPaused) { this.audioImg = iconPaused
}
afterAudioPlay()
},
onError(e) {
uni.showToast({ title: '音频加载失败', icon: 'none'
}) throw new Error(e.errMsg, e.errCode)
},
fmtSecond(sec) { const { min, second } = formatSecondToHHmmss(sec) return `${min}:${second}`
}
},复制代码
同样的scss文件
<style lang="scss" scoped>.custom-audio { border-radius: 8vw; border: #CCC 1px solid; background: #F3F6FC; color: #333; display: flex; flex-flow: row nowrap; align-items: center; justify-content: space-between; padding: 2vw; font-size: 14px;
.audio-btn { width: 10vw; height: 10vw; white-space: nowrap; display: flex; align-items: center; justify-content: center;
}
}
</style>复制代码
最后

各位有遇到什么问题或有什么建议的可以跟我讨论哟~
详解uni-app(vue)基于InnerAudioContext封装一个基本的音频组件
—–文章转载自PHP中文网如有侵权请联系admin#tyuanma.cn删除
php中的array函数有什么用
转载请注明来源:详解uni-app(vue)基于InnerAudioContext封装一个基本的音频组件_编程技术_编程开发技术教程
本文永久链接地址:https://www.ymkuzhan.com/986.html
本文永久链接地址:https://www.ymkuzhan.com/986.html
下载声明:
本站资源如无特殊说明默认解压密码为www.ymkuzhan.com建议使用WinRAR解压; 本站资源来源于用户分享、互换、购买以及网络收集等渠道,本站不提供任何技术服务及有偿服务,资源仅提供给大家学习研究请勿作它用。 赞助本站仅为维持服务器日常运行并非购买程序及源码费用因此不提供任何技术支持,如果你喜欢该程序,请购买正版! 版权声明:
下载本站资源学习研究的默认同意本站【版权声明】若本站提供的资源侵犯到你的权益,请提交版权证明文件至邮箱ymkuzhan#126.com(将#替换为@)站长将会在三个工作日内为您删除。 免责声明:
您好,本站所有资源(包括但不限于:源码、素材、工具、字体、图像、模板等)均为用户分享、互换、购买以及网络收集而来,并未取得原始权利人授权,因此禁止一切商用行为,仅可用于个人研究学习使用。请务必于下载后24小时内彻底删除,一切因下载人使用所引起的法律相关责任,包括但不限于:侵权,索赔,法律责任,刑事责任等相关责任,全部由下载人/使用人,全部承担。以上说明,一经发布视为您已全部阅读,理解、同意以上内容,如对以上内容持有异议,请勿下载,谢谢配合!支持正版,人人有责,如不慎对您的合法权益构成侵犯,请联系我们对相应内容进行删除,谢谢!




