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

Conversation

sarayourfriend
Copy link
Contributor

Fixes

Fixes #289 by @zackkrida

Description

Adds a new played state to the AudioTrack triggered when the current time of the audio matches or exceeds the duration of the audio. When seeking during this state, it toggles back to paused. When hitting the play/pause button during the state, it will restart the audio from the beginning.

Gravacao.de.Tela.2021-10-08.as.08.43.26.mov

I tried to implement this in the least intrusive way possible... In the course of that I added two new events to the AudioControlled (finished and seeked). The seeked event is probably the most problematic as it doesn't emit the timestamp or anything like that, which you could argue would make sense for a seeked event to do. But it wasn't necessary for this particular application of that event I was sort of thinking of a YAGNI kind of thing, to keep the event as minimal as possible for the time being. If in the future we need to add the timestamp it would be trivial to do (but I'm also open to adding it now if folks think it would make more sense that way, even though it's not needed for this particular instance).

I tried to think of a few different approaches to solving this problem but then all eventually hit dead ends... this was the least intrusive version of these changes I could come up with.

Testing Instructions

Checkout this PR and run npm run storybook and navigate to /?path=/story/components-audio-track--default-story. Play around with the audio track and the new replay functionality and verify that the existing behavior continues to work (especially seeking) and that the new replay behavior works and makes sense.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@sarayourfriend sarayourfriend added 🕹 aspect: interface Concerns end-users' experience with the software 🛠 goal: fix Bug fix labels Oct 8, 2021
Comment on lines +90 to +93
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
}
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.

Comment on lines +92 to +96
const handleSeeked = () => {
if (status.value === 'played') {
status.value = 'paused'
}
}
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!

@@ -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.

👍

@zackkrida zackkrida marked this pull request as ready for review October 11, 2021 12:55
@zackkrida zackkrida requested a review from a team as a code owner October 11, 2021 12:55
@zackkrida zackkrida requested review from zackkrida and obulat October 11, 2021 12:55
@zackkrida
Copy link
Member

@sarayourfriend You can go ahead and write the tests, this is a great approach to this work and you made improvements across the board 👍

@sarayourfriend
Copy link
Contributor Author

sarayourfriend commented Oct 11, 2021

@zackkrida I had started trying to write tests for this on Friday but... I ran into a big issue, which is that I'm not sure how to set up the audio element inside the tests with an actual duration and currentTime value that the component can run against without making a network request 😱 I was going to look into whether we could include a test file into the repository that axios-mock some kind of network mocking interface or something like that could respond with... but it might require a special loader or some out of the ordinary file reading logic before the tests run. But I'll try it out!

@sarayourfriend
Copy link
Contributor Author

Unfortunately it's not possible to actually write unit tests against this... because we're operating against a real HTMLAudioElement, which isn't fully implemented by JSDom. For example, it doesn't actually load or play the media (meaning we can't test that the play/pause/replay functionality works outside of E2E tests which run in a proper browser environment)

@zackkrida
Copy link
Member

@sarayourfriend in that case...LGTM!

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

It works great!

@sarayourfriend sarayourfriend merged commit 7d517f9 into main Oct 12, 2021
@sarayourfriend sarayourfriend deleted the fix/replay-button branch October 12, 2021 15:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🕹 aspect: interface Concerns end-users' experience with the software 🛠 goal: fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] {Audio} Play button doesn't reset when audio track ends
3 participants