add shared callback impl between mpv and jukebox players

This commit is contained in:
Drew Weymouth
2024-09-01 08:45:09 -07:00
parent c2efb048e2
commit faf067ce7e
4 changed files with 80 additions and 60 deletions
+65
View File
@@ -82,3 +82,68 @@ func (r ReplayGainMode) String() string {
return "no"
}
}
type BasePlayerCallbackImpl struct {
onPaused []func()
onStopped []func()
onPlaying []func()
onSeek []func()
onTrackChange []func()
}
// Registers a callback which is invoked when the player transitions to the Paused state.
func (p *BasePlayerCallbackImpl) OnPaused(cb func()) {
p.onPaused = append(p.onPaused, cb)
}
// Registers a callback which is invoked when the player transitions to the Stopped state.
func (p *BasePlayerCallbackImpl) OnStopped(cb func()) {
p.onStopped = append(p.onStopped, cb)
}
// Registers a callback which is invoked when the player transitions to the Playing state.
func (p *BasePlayerCallbackImpl) OnPlaying(cb func()) {
p.onPlaying = append(p.onPlaying, cb)
}
// Registers a callback which is invoked whenever a seek event occurs.
func (p *BasePlayerCallbackImpl) OnSeek(cb func()) {
p.onSeek = append(p.onSeek, cb)
}
// Registers a callback which is invoked when the currently playing track changes,
// or when playback begins at any time from the Stopped state.
// Callback is invoked with the index of the currently playing track (zero-based).
func (p *BasePlayerCallbackImpl) OnTrackChange(cb func()) {
p.onTrackChange = append(p.onTrackChange, cb)
}
func (p *BasePlayerCallbackImpl) InvokeOnPaused() {
for _, cb := range p.onPaused {
cb()
}
}
func (p *BasePlayerCallbackImpl) InvokeOnPlaying() {
for _, cb := range p.onPlaying {
cb()
}
}
func (p *BasePlayerCallbackImpl) InvokeOnStopped() {
for _, cb := range p.onStopped {
cb()
}
}
func (p *BasePlayerCallbackImpl) InvokeOnSeek() {
for _, cb := range p.onSeek {
cb()
}
}
func (p *BasePlayerCallbackImpl) InvokeOnTrackChange() {
for _, cb := range p.onTrackChange {
cb()
}
}