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

Callback or Signal for Playback complete #230

Open
JackMordaunt opened this issue Jan 5, 2024 · 4 comments
Open

Callback or Signal for Playback complete #230

JackMordaunt opened this issue Jan 5, 2024 · 4 comments

Comments

@JackMordaunt
Copy link

Instead of polling the player, perhaps it would be better to offer either a callback or a channel style API.

    // We can wait for the sound to finish playing using something like this
    for player.IsPlaying() {
        time.Sleep(time.Millisecond)
    }
// Channel style.
<-player.Done()

// Callback style.
player.OnDone(func() {...})
@JackMordaunt
Copy link
Author

You can get underneath the player with a custom io.Reader, but it is a bit annoying - but maybe a good route?

// TriggerReader invokes a callback when EOF is reached.
type TriggerReader struct {
	r  io.Reader
	cb func()
}

func (t *TriggerReader) Read(p []byte) (n int, err error) {
	n, err = t.r.Read(p)
	if err == io.EOF {
		t.Callback()
	}
	return n, err
}

func (t *TriggerReader) Callback() {
	if t.cb != nil {
		t.cb()
	}
}
done := make(chan struct{})
data := []byte{...}
src := bytes.NewReader(data)
src = &TriggerReader{r: src, cb: func() {close(done)}}
p := ctx.NewPlayer(src)
p.Play()
<-done

@hajimehoshi
Copy link
Collaborator

I was wondering why you need to detect the timing when the player ends in the first place.

@JackMordaunt
Copy link
Author

JackMordaunt commented Jan 8, 2024

I have a program that wants to exit once playback is finished, but you could also imagine advancing a track in a playlist, or similar.

@MarkKremer
Copy link

Another use case could be that we want to wait for the player to finish and all the samples be sent to the driver before suspending so that the last couple of samples don't get lost.

I was discussing this regarding Beep from this comment down: gopxl/beep#137 (comment)

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

No branches or pull requests

3 participants