詳解uni-app(vue)基於InnerAudioContext封裝一個基本的音訊組件
相關學習推薦:#微信小程式開發
原由
同樣的是因為小程式官方不維護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>复制代码
登入後複製最後

各位有遇到什麼問題或有什麼建議的可以跟我討論喲~
<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欄位~
以上是詳解uni-app(vue)基於InnerAudioContext封裝一個基本的音訊組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

VSCode中如何開發uni-app?以下這篇文章跟大家分享一下VSCode中開發uni-app的教學課程,這可能是最好、最詳細的教學了。快來看看!

如何利用uniapp開發一個貪吃蛇小遊戲?以下這篇文章就手把手帶大家在uniapp中實現貪吃蛇小遊戲,希望對大家有幫助!

uni-app接口,全域方法封裝1.在根目錄建立一個api文件,在api資料夾中建立api.js,baseUrl.js和http.js檔案2.baseUrl.js檔案程式碼exportdefault"https://XXXX .test03.qcw800.com/api/"3.http.js檔案程式碼exportfunctionhttps(opts,data){lethttpDefaultOpts={url:opts.url,data:data,method:opts.method

這篇文章手把手帶大家開發一個uni-app日曆插件,介紹下一個日曆插件是如何從開發到發布的,希望對大家有幫助!

這篇文章為大家帶來了關於uniapp的相關知識,其中主要整理了實現多選框的全選功能的相關問題,無法實現全選的原因是動態修改checkbox的checked字段時,界面上的狀態能夠即時變化,但無法觸發checkbox-group的change事件,下面一起來看一下,希望對大家有幫助。

uniapp怎麼實作scroll-view下拉載入?以下這篇文章聊聊uniapp微信小程式scroll-view的下拉加載,希望對大家有幫助!

這篇文章為大家帶來了關於uniapp的相關知識,其中主要介紹了怎麼用uniapp實現撥打電話並且還能同步錄音的功能,感興趣的朋友一起來看一下吧,希望對大家有幫助。
