相關學習推薦:#微信小程式開發
原由
同樣的是因為小程式官方不維護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欄位~
以上是詳解uni-app(vue)基於InnerAudioContext封裝一個基本的音訊組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了有關移動和網絡平台的調試策略,突出顯示了Android Studio,Xcode和Chrome DevTools等工具,以及在OS和性能優化的一致結果的技術。

文章討論了用於Uniapp開發的調試工具和最佳實踐,重點關注Hbuilderx,微信開發人員工具和Chrome DevTools等工具。

本文討論了跨多個平台的Uniapp應用程序的端到端測試。它涵蓋定義測試方案,選擇諸如Appium和Cypress之類的工具,設置環境,寫作和運行測試,分析結果以及集成

本文討論了針對Uniapp應用程序的各種測試類型,包括單元,集成,功能,UI/UX,性能,跨平台和安全測試。它還涵蓋了確保跨平台兼容性,並推薦Jes等工具

本文討論了UNIAPP開發中的共同績效抗模式,例如過度的全球數據使用和效率低下的數據綁定,並提供策略來識別和減輕這些問題,以提高應用程序性能。

本文討論了通過壓縮,響應式設計,懶惰加載,緩存和使用WebP格式來優化Uniapp中的圖像,以更好地進行Web性能。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版
好用的JavaScript開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。