search
HomeWeb Front-enduni-appDetailed explanation of uni-app (vue) encapsulating a basic audio component based on InnerAudioContext

Detailed explanation of uni-app (vue) encapsulating a basic audio component based on InnerAudioContext

Related learning recommendations: WeChat Mini Program Development

## The reason

is also because the official mini program does not maintain the

audio component

Requirements and restrictions of audio components

1. Click to play or pause

2. Display the playback progress and total duration
3. Display the current audio status (paused/playing/loading) through icon changes
4. Refresh the component status when the page audio is updated
5. Global There is and only one audio in the playing state
6. Automatically stop playing and destroy the audio instance after leaving the page

Materials/Properties/Methods

Let’s get started

uni-app Vue

    Similarly construct
  • DOMstructure
  • <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>复制代码
    Define accepted components
  • props: {  audioSrc: {    type: String,    default: &#39;&#39;
      },
    },复制代码
    Define the operations related to the initialization of the
  • CustomAudio component, and add some behaviors to the callback of innerAudioContext (the pitfalls we stepped on in the previous Taro article will be directly coded here) )
  • 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}`
      }
    },复制代码
Same

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>复制代码

Finally

Detailed explanation of uni-app (vue) encapsulating a basic audio component based on InnerAudioContext
If you encounter any problems or have any suggestions, you can discuss them with me~

If you want to know other excellent articles, please visit the

uni-app column~

The above is the detailed content of Detailed explanation of uni-app (vue) encapsulating a basic audio component based on InnerAudioContext. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools