Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add replay action and played state to audio track #301

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/assets/icons/replay.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions src/components/AudioTrack/AudioController.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default {
status: {
type: String,
required: true,
validator: (val) => ['playing', 'paused'].includes(val),
validator: (val) => ['playing', 'paused', 'played'].includes(val),
},
/**
* the CSS classes to apply on the waveform; This can take any form
Expand All @@ -84,9 +84,14 @@ export default {
// Sync status from parent to player and store
watch(
() => props.status,
(status) => {
(status, prevStatus) => {
if (!audioEl.value) return

if (prevStatus === 'played' && status === 'playing') {
// If going from played to playing, then reset the time to the beginning. Let the regular logic handle actually triggering the playing of the audio
audioEl.value.currentTime = 0
}
Comment on lines +90 to +93
Copy link
Member

@zackkrida zackkrida Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. This makes a lot of sense to me, that internally this only cares about the current playtime rather than explicitly triggering events. "If we're transitioning from 'playing' to 'played', set the timestamp to 0" is quite logical.


switch (status) {
case 'playing':
audioEl.value.play()
Expand Down Expand Up @@ -146,6 +151,10 @@ export default {

currentTime.value = audioEl.value.currentTime
duration.value = audioEl.value.duration

if (currentTime.value >= duration.value) {
emit('finished')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}
const updateTimeLoop = () => {
updateTime()
Expand All @@ -169,6 +178,7 @@ export default {
if (audioEl.value && duration.value) {
audioEl.value.currentTime = duration.value * frac
updateTime()
emit('seeked')
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/components/AudioTrack/AudioTrack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
v-bind="controllerProps"
:audio="audio"
@ready="handleReady"
@finished="status = 'played'"
@seeked="handleSeeked"
/>
</template>

Expand Down Expand Up @@ -87,6 +89,12 @@ export default {

const status = ref('paused')

const handleSeeked = () => {
if (status.value === 'played') {
status.value = 'paused'
}
}
Comment on lines +92 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!


/* Metadata readiness */

const isReady = ref(false)
Expand All @@ -104,6 +112,7 @@ export default {

return {
status,
handleSeeked,

isReady,
handleReady,
Expand Down
37 changes: 27 additions & 10 deletions src/components/AudioTrack/PlayPause.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,35 @@
aria-hidden="true"
focusable="false"
>
<use v-if="isPlaying" :href="`${icons.pause}#icon`" />
<use v-else :href="`${icons.play}#icon`" />
<use :href="`${icon}#icon`" />
</svg>
</button>
</template>

<script>
import playIcon from '~/assets/icons/play.svg'
import pauseIcon from '~/assets/icons/pause.svg'
import replayIcon from '~/assets/icons/replay.svg'

/**
* @param {'playing' | 'paused' | 'played'} status
*/
const getLabelFromStatus = (status) => {
switch (status) {
case 'playing':
return 'play-pause.pause'
case 'paused':
return 'play-pause.play'
case 'played':
return 'play-pause.replay'
}
}

const STATUS_TO_ICON = {
playing: pauseIcon,
paused: playIcon,
played: replayIcon,
}

/**
* Displays the control for switching between the playing and paused states of
Expand All @@ -38,21 +58,18 @@ export default {
*/
status: {
type: String,
validator: (val) => ['playing', 'paused'].includes(val),
validator: (val) => ['playing', 'paused', 'played'].includes(val),
},
},
data: () => ({
icons: {
play: playIcon,
pause: pauseIcon,
},
}),
computed: {
isPlaying() {
return this.status === 'playing'
},
label() {
return this.$t(this.isPlaying ? 'play-pause.pause' : 'play-pause.play')
return this.$t(getLabelFromStatus(this.status))
},
icon() {
return STATUS_TO_ICON[this.status]
},
},
methods: {
Expand Down