InnerAudioContext に基づいて基本的なオーディオ コンポーネントをカプセル化する uni-app (vue) の詳細な説明

coldplay.xixi
リリース: 2020-09-19 16:38:10
転載
4621 人が閲覧しました

InnerAudioContext に基づいて基本的なオーディオ コンポーネントをカプセル化する uni-app (vue) の詳細な説明

関連する学習の推奨事項: WeChat ミニ プログラム開発

の理由は、公式のミニ プログラムが audio コンポーネント

オーディオ コンポーネントの要件と制限事項

1 を維持していないためでもあります。クリックして再生します。または一時停止
2. 再生の進行状況と合計時間を表示します
3. アイコンの変化によって現在のオーディオのステータス (一時停止/再生中/読み込み中) を表示します
4. ページのオーディオが更新されたときにコンポーネントのステータスを更新します
5. グローバル 再生状態にあるオーディオは 1 つだけです
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}`
      }
    },复制代码
    ログイン後にコピー
同じ

scssfile
<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>复制代码
ログイン後にコピー
最後に

InnerAudioContext に基づいて基本的なオーディオ コンポーネントをカプセル化する uni-app (vue) の詳細な説明
# #問題が発生したり、提案がある場合は、私と話し合ってください~

他の優れた記事を知りたい場合は、

uni にアクセスしてください。 -app 列~

以上がInnerAudioContext に基づいて基本的なオーディオ コンポーネントをカプセル化する uni-app (vue) の詳細な説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:juejin.im
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!