Skip to content

Commit

Permalink
WordPress#692) Set duration after audio is loaded
Browse files Browse the repository at this point in the history
Before this commit the duration would be set based on the audio context
state as the page was loaded. Since the audio file is never loaded by
the time we check for the duration of the audio object, it is always
null thus defaulting to the metadata for audio duration always.

This commit adds a `durationchange` listener while initializing the
audio object and sets the duration in a new `audioDuration` property
which the computed `duration` property watches.
  • Loading branch information
demophoon committed Feb 18, 2022
1 parent f3aa9f2 commit 9f22864
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/components/VAudioTrack/VAudioTrack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default defineComponent({
const status = ref('paused')
const currentTime = ref(0)
const audioDuration = ref(null)
const initLocalAudio = () => {
// Preserve existing local audio if we plucked it from the global active audio
Expand All @@ -133,6 +134,7 @@ export default defineComponent({
localAudio.addEventListener('pause', setPaused)
localAudio.addEventListener('ended', setPlayed)
localAudio.addEventListener('timeupdate', setTimeWhenPaused)
localAudio.addEventListener('durationchange', setDuration)
/**
* Similar to the behavior in the global audio track,
Expand Down Expand Up @@ -216,6 +218,9 @@ export default defineComponent({
}
}
}
const setDuration = () => {
audioDuration.value = localAudio?.duration
}
/**
* If we're transforming the globally active audio
Expand All @@ -238,6 +243,7 @@ export default defineComponent({
localAudio.removeEventListener('pause', setPaused)
localAudio.removeEventListener('ended', setPlayed)
localAudio.removeEventListener('timeupdate', setTimeWhenPaused)
localAudio.removeEventListener('durationchange', setDuration)
if (
route.value.params.id == props.audio.id ||
Expand Down Expand Up @@ -286,7 +292,7 @@ export default defineComponent({
/* Timekeeping */
const duration = computed(
() => (localAudio?.duration ?? props.audio?.duration ?? 0) / 1e3 // seconds
() => audioDuration.value ?? props.audio?.duration / 1e3 ?? 0 // seconds
)
const message = computed(() => store.state.active.message)
Expand Down
10 changes: 8 additions & 2 deletions src/components/VAudioTrack/VGlobalAudioTrack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default defineComponent({
const status = ref('paused')
const currentTime = ref(0)
const audioDuration = ref(null)
const setPlaying = () => {
status.value = 'playing'
Expand All @@ -133,6 +134,9 @@ export default defineComponent({
}
}
}
const setDuration = () => {
audioDuration.value = activeAudio.obj.value?.duration
}
const updateTimeLoop = () => {
if (activeAudio.obj.value && status.value === 'playing') {
Expand All @@ -149,7 +153,9 @@ export default defineComponent({
audio.addEventListener('pause', setPaused)
audio.addEventListener('ended', setPlayed)
audio.addEventListener('timeupdate', setTimeWhenPaused)
audio.addEventListener('durationchange', setDuration)
currentTime.value = audio.currentTime
audioDuration.value = audio.duration
/**
* By the time the `activeAudio` is updated and a rerender
Expand Down Expand Up @@ -179,6 +185,7 @@ export default defineComponent({
audio.removeEventListener('pause', setPaused)
audio.removeEventListener('ended', setPlayed)
audio.removeEventListener('timeupdate', setTimeWhenPaused)
audio.removeEventListener('durationchange', setDuration)
})
},
{ immediate: true }
Expand All @@ -190,8 +197,7 @@ export default defineComponent({
/* Timekeeping */
const duration = computed(
() =>
(activeAudio.obj.value?.duration ?? props.audio?.duration ?? 0) / 1e3
() => audioDuration.value ?? props.audio?.duration / 1e3 ?? 0
) // seconds
const message = computed(() => store.state.active.message)
Expand Down

0 comments on commit 9f22864

Please sign in to comment.