1.2.0 • Published 5 months ago

1llest-waveform-vue v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Description

This component is written using the native Web Audio API, and does not use any dependencies other than vue3 in the production environment. Of course, this means: if your target browser does not support the features of the web audio api, then my Plugins will also not apply. You can go to Browser compatibility to see the AudioContext line to check whether it is compatible with the target browser

Example - Live Demo

Start

Install

npm install 1llest-waveform-vue

Usage

Global component

// main.ts
import { createApp } from "vue"
import App from "./App.vue"

import IllestWaveform from "1llest-waveform-vue"
import "1llest-waveform-vue/lib/style.css"

const app = createApp(App)

app.use(IllestWaveform)
app.mount("#app")

Local component

// example.vue
import { IllestWaveform } from "1llest-waveform-vue"
import "1llest-waveform-vue/lib/style.css"

Component

<template>
  <IllestWaveform
    ref="waveformRef"
    v-bind="waveOptions"
    @on-init="initHandler"
    @on-fetched="fetchedHandler"
    @on-ready="readyHandler"
    @on-play="(v: boolean) => (playing = v)"
    @on-pause="(v: boolean) => (playing = v)"
    @on-finish="finishHandler"
    @on-click="clickHandler"
  />
  <div>{{ currentTime }} - {{ durationTime }}</div>
</template>

<script setup lang="ts">
  import { onMounted, reactive, ref, watchEffect } from "vue"
  import type { Ref } from "vue"
  import { IllestWaveform } from "1llest-waveform-vue"
  import type { IllestWaveformProps } from "1llest-waveform-vue"
  import "1llest-waveform-vue/lib/style.css"
  
  const waveOptions = reactive<IllestWaveformProps>({
    url: "example.mp3"
  })

  const waveformRef = ref<typeof IllestWaveform | null>(null)

  onMounted(() => {
    getCurrentTime()
  })

  const init = ref(false)
  const fetched = ref(false)
  const playing = ref(false)
  const finished = ref(false)
  const ready = ref(false)
  const currentTime = ref("0:00")
  const durationTime = ref("0:00")

  const initHandler = (v: boolean) => {
    init.value = v
  }
  
  const fetchedHandler = (v: boolean) => {
    fetched.value = v
  }

  const readyHandler = (v: boolean) => {
    ready.value = v
    getDuration()
  }
  
  const finishHandler = (v: boolean) => {
    finished.value = v
  }

  const clickHandler = (el: Ref<HTMLElement>) => {
    console.log(el)
  }

  const play = () => {
    waveformRef.value!.play()
  }

  const replay = () => {
    waveformRef.value!.replay()
  }

  const pause = () => {
    waveformRef.value!.pause()
  }

  const getCurrentTime = () => {
    watchEffect(() => {
      const current = waveformRef.value!.getCurrentTime()
      currentTime.value = current
    })
  }

  const getDuration = () => {
    const duration = waveformRef.value!.getDuration()
    durationTime.value = duration
  }
</script>

Documentation

Component Props

propdescriptiontypedefault
urlthe url of the audio fileString-
requestOptionsthe object passed to the fetch request functionRequestInit-
lineWidththe width of each vertical line that makes up the waveformNumber0.5
lineCapthe style at the end of each vertical line that makes up the waveformCanvasLineCapround
lineColorthe color of each vertical line that makes up the waveformString#5e5e5e
samplingRateindicates your audio sampling rate. The larger the value, the more lines the waveform will present and the higher the accuracy. But this value is not recommended to be too large, because too large a value will slow down rendering efficiency, the recommended value is between 8000 - 44100Number22050
cursorWidthindicates your cursor widthNumber2
cursorColorthe color of your cursorString#fff
maskColorthe color of the waveform mask layerString#fff
lazywhether to enable lazy loading mode, if you want to display multiple waveforms as a list, this property is very usefulBooleantrue
skeletonwhether to enable the skeleton during waveform loadingBooleantrue
skeletonColorthe color of the skeletonString#232323
interactindicates whether you want the user to interact with the waveformBooleantrue
fadeachieve fade-in and fade-out effects when playing and pausing audio, this can give the user a smoother listening experienceBooleantrue

Events

When using the following events, you need to add the on- prefix in front, such as @on-init="initHandler"

eventdescriptionparams
initthe hook event before the waveform starts to initializeBoolean
fetchedthe hook event after accepting the audio fileBoolean
readythe hook event triggered after the waveform completes all initialization and rendering to the pageBoolean
playevent fired when playback startsBoolean
pauseevent fired when playback is pausedBoolean
finishthe event triggered when the playback is completed (the playback completion refers to the completion of the entire audio)Boolean
clickevent triggered when waveform is clickedRef<HTMLElement>

Methods

You can call these methods directly on the waveform component instance, such like waveform_ref.value.play()

methoddescriptionreturn
playtrigger the playback method of the waveform so that it starts playing the current audio-
pausetrigger the pause method of the waveform to make it pause playback-
replaythis method can restart playing the current audio again-
getCurrentTimethis method can get the current playing time. If you want to get the current playback time in real time, you can wrap it in the watchEffect hookstring
getDurationthis method can get the duration of the current audio, but this method must be placed after the ready hook event is triggered to get the correct durationstring

Contributing

Contributions to the project are welcome! If you find a bug or have an idea for a new feature, please submit an issue or pull request.

License

MIT License © 2023-Present leyoonafr

1.2.0

5 months ago

1.1.0

8 months ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago