0

0

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

coldplay.xixi

coldplay.xixi

发布时间:2020-09-19 16:32:04

|

5172人浏览过

|

来源于juejin

转载

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

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

原由

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

音频组件的要求与限制

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

材料/属性/方法

让我们开始吧

Vozo
Vozo

Vozo是一款强大的AI视频编辑工具,可以帮助用户轻松重写、配音和编辑视频。

下载

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 <= 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文件

复制代码

最后

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

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

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

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Java 教程
Java 教程

共578课时 | 36.9万人学习

国外Web开发全栈课程全集
国外Web开发全栈课程全集

共12课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号