Skip to content

player.IsPlaying() returns false before hardware finishes playing #237

Description

@ZYDeng91

Oof, my audio keeps getting cut off before the last second.

If I guessed correctly, player.IsPlaying() does not account for the hardware and will return false once it has emptied its internal buffer. And makes my program exit prematurely.

Apart from sleeping a duration of BufferSize, is there any way to tell if the player really finished playing?

Activity

  1. added this to the v3.3.0 milestone on May 29, 2024
  2. hajimehoshi commented on May 29, 2024

    @hajimehoshi
    Member

    Unfortunately there is no way to detect whether the buffer that is sent to hardware is already played or not. Let me think...

  3. gucio321 commented on Jul 21, 2024

    @gucio321
    Contributor

    maybe guss duration of the buffer somehow and use time.Sleep? (I think its how all of us workaround that at the moment)

  4. ZYDeng91 commented on Jul 21, 2024

    @ZYDeng91
    ContributorAuthor

    maybe guss duration of the buffer somehow and use time.Sleep

    I think that's what I meant by "sleeping a duration of BufferSize". Was wondering how it can be done more elegantly upstream.

    And uh, not really a workaround but setting the buffer size to default does it for me.

  5. MarkKremer commented on Aug 10, 2024

    @MarkKremer

    I think it would be interesting to have per-driver guesstimates about the hardware playback position. It could be interesting to figure out what AudioContext.currentTime does.

    One idea, for example, is that ALSA works with periods. Say that at the moment when we just finished sending a period of audio to ALSA, and given that n periods are filled, we could make an assumption that at that time the speaker is playing sample number x. Using the samplerate we can extrapolate the sample number at later times. But keep updating the measurements.

    Then if you need the hardware position of the player, just account for the player's buffer size in between.

    This could later be expanded to allow other scheduling based playback.

    I don't have time for it right now but I would be happy to make a POC for ALSA at some point, and maybe Windows.


    I think it would be good to have a difference between player.IsPlaying() and player.HardwareIsPlaying() for different use-cases. If you call player.Play(), player.HardwareIsPlaying() will still return false for a number of milliseconds.

  6. modified the milestones: v3.3.0, v3.4.0 on Oct 6, 2024
  7. modified the milestones: v3.4.0, v3.5.0 on Oct 4, 2025
  8. modified the milestones: v3.5.0, v3.6.0 on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions