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

詳解uni-app(vue)基於InnerAudioContext封裝一個基本的音訊組件

coldplay.xixi
coldplay.xixi轉載
2020-09-19 16:32:044842瀏覽

詳解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.im。如有侵權,請聯絡admin@php.cn刪除