搜尋
首頁web前端uni-app詳解uni-app(vue)基於InnerAudioContext封裝一個基本的音訊組件

詳解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 !== &#39;&#39;" @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: &#39;&#39;
      },
    },复制代码
    定義
  • CustomAudio元件的初始化相關的操作,並為innerAudioContext的回呼添加一些行為(之前Taro那篇我們踩過的坑這裡就直接上程式碼了)
  • import { formatSecondToHHmmss, afterAudioPlay, beforeAudioRecordOrPlay } from &#39;../../lib/Utils&#39;const iconPaused = &#39;../../static/images/icon_paused.png&#39;const iconPlaying = &#39;../../static/images/icon_playing.png&#39;const iconStop = &#39;../../static/images/icon_stop.png&#39;const iconLoading = &#39;../../static/images/icon_loading.gif&#39;// ...data() {  return {    audioCtx: null, // 音频上下文
        duration: 0, // 音频总时长
        currentTime: 0, // 音频当前播放的时长
        audioImg: iconLoading, // 默认状态为加载中
      }
    },watch: {  audioSrc: {
        handler(newSrc, oldSrc) {      console.log(&#39;watch&#39;, 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: &#39;无效音源,请先录音&#39;,      icon: &#39;none&#39;
        })
      },
      playOrStopAudio() {    if (this.audioCtx === null) {      this.audioCtx = uni.createInnerAudioContext()      this.audioCtx.src = this.audioSrc      this.bindAuidoCallback(this.audioCtx)
        }    if (this.audioCtx.paused) {      if (beforeAudioRecordOrPlay(&#39;play&#39;)) {        this.audioCtx.play()        this.audioImg = iconPlaying
          }
        } else {      this.audioCtx.pause()
          afterAudioPlay()      this.audioImg = iconPaused
        }
      },
      onTimeUpdate(e) {    console.log(&#39;onTimeUpdate&#39;, 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(&#39;onCanplay&#39;, e)
      },
      onWaiting(e) {    if (this.audioImg !== iconLoading) {      this.audioImg = iconLoading
        }
      },
      onPlay(e) {    console.log(&#39;onPlay&#39;, 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(&#39;onPause&#39;, e)    this.audioImg = iconPaused
      },
      onEnded(e) {    console.log(&#39;onEnded&#39;, e)    if (this.audioImg !== iconPaused) {      this.audioImg = iconPaused
        }
        afterAudioPlay()
      },
      onError(e) {
        uni.showToast({      title: &#39;音频加载失败&#39;,      icon: &#39;none&#39;
        })    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封裝一個基本的音訊組件
各位有遇到什麼問題或有什麼建議的可以跟我討論喲~

想了解其他精品文章,敬請訪問

uni-app欄位~

以上是詳解uni-app(vue)基於InnerAudioContext封裝一個基本的音訊組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:juejin。如有侵權,請聯絡admin@php.cn刪除
您如何在不同平台(例如移動,Web)上調試問題?您如何在不同平台(例如移動,Web)上調試問題?Mar 27, 2025 pm 05:07 PM

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

哪些調試工具可用於Uniapp開發?哪些調試工具可用於Uniapp開發?Mar 27, 2025 pm 05:05 PM

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

您如何為Uniapp應用程序執行端到端測試?您如何為Uniapp應用程序執行端到端測試?Mar 27, 2025 pm 05:04 PM

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

您可以在Uniapp應用程序中執行哪些不同類型的測試?您可以在Uniapp應用程序中執行哪些不同類型的測試?Mar 27, 2025 pm 04:59 PM

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

Uniapp中有哪些常見的性能反版?Uniapp中有哪些常見的性能反版?Mar 27, 2025 pm 04:58 PM

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

您如何使用分析工具來識別uniapp中的性能瓶頸?您如何使用分析工具來識別uniapp中的性能瓶頸?Mar 27, 2025 pm 04:57 PM

本文討論了使用分析工具來識別和解決Uniapp中的性能瓶頸,重點是設置,數據分析和優化。

您如何在Uniapp中優化網絡請求?您如何在Uniapp中優化網絡請求?Mar 27, 2025 pm 04:52 PM

本文討論了在UNIAPP中優化網絡請求的策略,重點是減少延遲,實施緩存以及使用監視工具來增強應用程序性能。

如何優化Uniapp中的Web性能的圖像?如何優化Uniapp中的Web性能的圖像?Mar 27, 2025 pm 04:50 PM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

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

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SecLists

SecLists

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

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

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