搜索
首页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尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境