Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ZYDeng91 opened this issue May 29, 2024 · 4 comments
Open

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

ZYDeng91 opened this issue May 29, 2024 · 4 comments
Labels
Milestone

Comments

@ZYDeng91
Copy link
Contributor

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?

@hajimehoshi hajimehoshi added this to the v3.3.0 milestone May 29, 2024
@hajimehoshi
Copy link
Member

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

@gucio321
Copy link
Contributor

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

@ZYDeng91
Copy link
Contributor Author

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.

@MarkKremer
Copy link

MarkKremer commented Aug 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants