Skip to content

Commit 1b01711

Browse files
committed
feat: add fetched event
1 parent 7b7df5d commit 1b01711

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

example/src/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const toggleDark = useToggle(isDark)
1717
const items = reactive<Props[]>([
1818
{
1919
id: nanoid(),
20-
url: `${import.meta.env.BASE_URL}audios/loop-1.mp3`,
20+
// url: `${import.meta.env.BASE_URL}audios/loop-1.mp3`,
21+
url: 'https://wavesurfer-js.org/example/media/demo.wav',
2122
},
2223
{
2324
id: nanoid(),

example/src/components/Demo.vue

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ onMounted(() => {
4848
})
4949
5050
const init = ref(false)
51+
const fetched = ref(false)
5152
const playing = ref(false)
5253
const finished = ref(false)
5354
const ready = ref(false)
@@ -58,6 +59,10 @@ const initHandler = (v: boolean) => {
5859
init.value = v
5960
}
6061
62+
const fetchedHandler = (v: boolean) => {
63+
fetched.value = v
64+
}
65+
6166
const readyHandler = (v: boolean) => {
6267
ready.value = v
6368
getDuration()
@@ -103,6 +108,7 @@ const getDuration = () => {
103108
ref="waveformRef"
104109
v-bind="waveOptions"
105110
@on-init="initHandler"
111+
@on-fetched="fetchedHandler"
106112
@on-ready="readyHandler"
107113
@on-play="(v: boolean) => (playing = v)"
108114
@on-pause="(v: boolean) => (playing = v)"
@@ -112,6 +118,30 @@ const getDuration = () => {
112118
</div>
113119

114120
<div class="flex mt-2 items-end">
121+
<div
122+
class="flex items-center w-28"
123+
:class="`${fetched ? '' : 'animate-pulse'}`"
124+
>
125+
<span
126+
class="tag"
127+
:class="`${
128+
fetched ? 'bg-green-600 dark:bg-green-400' : 'bg-gray-400'
129+
}`"
130+
/>
131+
{{ fetched ? 'fetched' : 'fetching' }}
132+
</div>
133+
134+
<div
135+
class="flex items-center w-28"
136+
:class="`${ready ? '' : 'animate-pulse'}`"
137+
>
138+
<span
139+
class="tag"
140+
:class="`${ready ? 'bg-green-600 dark:bg-green-400' : 'bg-gray-400'}`"
141+
/>
142+
{{ ready ? 'ready' : 'rendering' }}
143+
</div>
144+
115145
<div class="ml-auto">
116146
<span class="text-neutral-400">{{ currentTime }}</span> /
117147
<strong>{{ durationTime }}</strong>
@@ -120,7 +150,7 @@ const getDuration = () => {
120150
<div class="ml-5">
121151
<button
122152
v-show="!playing && !finished"
123-
class="btn text-[#3e6bff]"
153+
class="text-[#3e6bff]"
124154
@click="play"
125155
>
126156
<PlayIcon />
@@ -129,14 +159,14 @@ const getDuration = () => {
129159

130160
<button
131161
v-show="playing && !finished"
132-
class="btn text-yellow-500"
162+
class="text-yellow-500"
133163
@click="pause"
134164
>
135165
<PauseIcon />
136166
<div>PAUSE</div>
137167
</button>
138168

139-
<button v-show="finished" class="btn text-green-500" @click="replay">
169+
<button v-show="finished" class="text-green-500" @click="replay">
140170
<ReplayIcon />
141171
<div>REPLAY</div>
142172
</button>
@@ -146,7 +176,11 @@ const getDuration = () => {
146176
</template>
147177

148178
<style scoped lang="scss">
149-
.btn {
179+
.tag {
180+
@apply inline-flex h-2 w-2 mr-2 rounded-full opacity-75;
181+
}
182+
183+
button {
150184
@apply flex items-center bg-neutral-200 dark:bg-neutral-700 px-5 py-1 rounded-sm;
151185
152186
& div {

src/components/IllestWaveform.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ async function init(): Promise<void> {
8585
8686
async function initAudioController(): Promise<void> {
8787
audioController = new AudioController(props)
88+
await audioController.fetchAudioFile()
89+
emits('onFetched', true)
8890
await audioController.setupAudio()
8991
watchIsFinish()
9092
}
@@ -177,6 +179,7 @@ function getDuration(): string {
177179
178180
const emits = defineEmits([
179181
'onInit', // start init hook
182+
'onFetched', // fetched audio file
180183
'onReady', // ready to play, rended
181184
'onPlay', // start play hook
182185
'onPause', // pause hook

src/modules/Audio.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class WebAudio {
1212
protected audioBuffer!: AudioBuffer
1313
protected audioBufferSourceNode!: AudioBufferSourceNode
1414
private filterData!: number[][]
15+
private response!: Response
1516

1617
constructor(props: IllestWaveformProps) {
1718
this.props = props
@@ -33,9 +34,16 @@ export default class WebAudio {
3334
this.createFilterData()
3435
}
3536

37+
public async fetchAudioFile(): Promise<void> {
38+
try {
39+
this.response = await fetch(this.props.url)
40+
} catch (error) {
41+
console.error(error)
42+
}
43+
}
44+
3645
private async createAudioBuffer(): Promise<void> {
37-
const response: Response = await fetch(this.props.url)
38-
const arrayBuffer: ArrayBuffer = await response.arrayBuffer()
46+
const arrayBuffer: ArrayBuffer = await this.response.arrayBuffer()
3947
this.audioBuffer = await this.audioCtx.decodeAudioData(arrayBuffer)
4048
}
4149

0 commit comments

Comments
 (0)