Reduce direct dependency on Player even more

This commit is contained in:
Michael Manganiello
2023-06-18 22:45:05 -03:00
parent 4629ede7a7
commit 7161dd40a9
3 changed files with 36 additions and 23 deletions
+18 -21
View File
@@ -116,12 +116,11 @@ type Player struct {
bgCancel context.CancelFunc
// callbacks
onPaused []func()
onStopped []func()
onPlaying []func()
onSeek []func()
onLoopModeChanged []func(string)
onTrackChange []func(int64)
onPaused []func()
onStopped []func()
onPlaying []func()
onSeek []func()
onTrackChange []func(int64)
}
// Returns a new player.
@@ -400,7 +399,12 @@ func (p *Player) PlayPause() error {
}
}
// Sets the loop mode of the player.
// Get the loop mode of the player.
func (p *Player) GetLoopMode() LoopMode {
return p.loopMode
}
// Set the loop mode of the player.
func (p *Player) SetLoopMode(mode LoopMode) error {
if !p.initialized {
return ErrUnitialized
@@ -413,22 +417,20 @@ func (p *Player) SetLoopMode(mode LoopMode) error {
switch mode {
case LoopNone:
p.mpv.SetOptionString("loop-playlist", "no")
if err := p.mpv.SetOptionString("loop-playlist", "no"); err != nil {
return err
}
case LoopAll:
p.mpv.SetOptionString("loop-playlist", "inf")
if err := p.mpv.SetOptionString("loop-playlist", "inf"); err != nil {
return err
}
}
p.loopMode = mode
defer func() {
for _, cb := range p.onLoopModeChanged {
cb(p.loopMode.String())
}
}()
return nil
}
// Changes the loop mode of the player to the next one.
// Change the loop mode of the player to the next one.
// Useful for toggling UI elements, to change modes without knowing the current player mode.
func (p *Player) SetNextLoopMode() error {
switch p.loopMode {
@@ -542,11 +544,6 @@ func (p *Player) OnSeek(cb func()) {
p.onSeek = append(p.onSeek, cb)
}
// Registers a callback which is invoked when the player enables queue repeat.
func (p *Player) OnLoopModeChanged(cb func(string)) {
p.onLoopModeChanged = append(p.onLoopModeChanged, 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).