Erlo

详解uni-app(vue)基于InnerAudioContext封装一个基本的音频组件

2020-09-24 06:30:28 发布   543 浏览  
页面报错/反馈
收藏 点赞
原文:https://www.php.cn/uni-app/460169.html

相关学习推荐:微信小程序开发

原由

同样的是因为小程序官方不维护audio组件了

音频组件的要求与限制

1、点击播放或者暂停
2、显示播放进度及总时长
3、通过图标变化显示当前音频所处状态(暂停/播放/加载中)
4、页面音频更新时刷新组件状态
5、全局有且只有一个音频处于播放状态
6、离开页面之后要自动停止播放并销毁音频实例

材料/属性/方法

让我们开始吧

uni-app Vue

  • 同样的先构造DOM结构

  
  无音源
  {{ fmtSecond(currentTime) }}/{{ fmtSecond(duration) }}复制代码
  • 定义接受的组件
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  0 && this.audioCtx.duration 

同样的scss文件

复制代码

最后

各位有遇到什么问题或有什么建议的可以跟我讨论哟~

想了解其他精品文章,敬请访问uni-app栏目~

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认