Documentation
¶
Overview ¶
Package player plays audio through libmpv and provides the player model.
Index ¶
- Constants
- Variables
- func LaunchCmd(p Player, url string, startTime float64, paused bool, httpHeaders []string, ...) tea.Cmd
- func LaunchGroupCmd(ctx context.Context, group *Group, paths []string, volume int, ...) tea.Cmd
- func QuitCmd(p Player) tea.Cmd
- func QuitGroupCmd(group *Group) tea.Cmd
- func SeekCmd(p Player, target float64) tea.Cmd
- func SetAppName(name string)
- func SetGroupPauseCmd(group *Group, paused bool, generation uint64) tea.Cmd
- func SetGroupVolumeCmd(group *Group, volume int, generation uint64) tea.Cmd
- func SetPauseCmd(p Player, paused bool) tea.Cmd
- func SetSpeedCmd(p Player, speed float64) tea.Cmd
- func SetVolumeCmd(p Player, vol int) tea.Cmd
- func TickAfterCmd(p Player, generation uint64, interval time.Duration) tea.Cmd
- func TickCmd(p Player, generation uint64) tea.Cmd
- func WatchGroupCmd(ctx context.Context, group *Group, generation uint64) tea.Cmd
- type CommandErrMsg
- type Group
- type GroupControlMsg
- type GroupEndedMsg
- type GroupReadyMsg
- type GroupStoppedMsg
- type Keys
- type MediaDownloadFunc
- type Model
- func (m Model) Init() tea.Cmd
- func (m Model) PlayPauseKey() key.Binding
- func (m Model) SeekBackKey() key.Binding
- func (m Model) SeekForwardKey() key.Binding
- func (m *Model) SetWidth(w int)
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) View() string
- func (m Model) VolumeDownKey() key.Binding
- func (m Model) VolumeUpKey() key.Binding
- type Mpv
- func (m *Mpv) Done() <-chan struct{}
- func (m *Mpv) GetDuration() (float64, error)
- func (m *Mpv) GetIdle() (bool, error)
- func (m *Mpv) GetPaused() (bool, error)
- func (m *Mpv) GetPosition() (float64, error)
- func (m *Mpv) Launch(url, startTime string, paused bool, httpHeaders []string, volume int) error
- func (m *Mpv) LaunchLoop(path string, paused bool, volume int) error
- func (m *Mpv) Quit() error
- func (m *Mpv) Seek(seconds float64) error
- func (m *Mpv) SetPause(paused bool) error
- func (m *Mpv) SetSpeed(speed float64) error
- func (m *Mpv) SetVolume(vol int) error
- func (m *Mpv) WatchEvents(generation uint64) tea.Cmd
- type Player
- type PlayerEndMsg
- type PlayerKeyMap
- type PlayerLaunchErrMsg
- type PlayerQuitMsg
- type PlayerReadyMsg
- type PositionMsg
- type SessionCache
- type StartPlayMsg
Constants ¶
const DefaultMediaCacheLimit int64 = 2 << 30
DefaultMediaCacheLimit bounds temporary media storage to 2 GiB.
const MaxSubliminalTracks = 8
MaxSubliminalTracks prevents accidental unbounded mpv player creation.
Variables ¶
var ErrClosed = errors.New("player closed")
ErrClosed reports that the player is gone: no file plays, or playback ended.
var ErrMediaCacheFull = errors.New("media cache size limit reached")
ErrMediaCacheFull reports that a download would exceed the session limit.
ErrPropertyUnavailable reports that mpv has no value for a property yet, for example while it opens a file or seeks.
Functions ¶
func LaunchCmd ¶
func LaunchCmd(p Player, url string, startTime float64, paused bool, httpHeaders []string, volume int, generation uint64) tea.Cmd
LaunchCmd starts playback at startTime in seconds. If paused is true, mpv starts paused. httpHeaders are the HTTP request headers of the stream. Returns PlayerReadyMsg when mpv has accepted the file. A file that fails to load ends the playback later.
func LaunchGroupCmd ¶ added in v0.6.0
func QuitGroupCmd ¶ added in v0.6.0
func SeekCmd ¶ added in v0.6.0
SeekCmd sends an absolute seek request to mpv by target seconds. It returns no message: Seek logs a failure, and the next position tick has the position of the player.
func SetAppName ¶ added in v0.6.0
func SetAppName(name string)
SetAppName sets the app name used for runtime file names. Call it once at startup, before playback starts.
func SetGroupPauseCmd ¶ added in v0.6.0
func SetGroupVolumeCmd ¶ added in v0.6.0
func SetPauseCmd ¶ added in v0.6.0
SetPauseCmd sets mpv's paused state.
func SetSpeedCmd ¶
SetSpeedCmd sends a speed change to mpv.
func SetVolumeCmd ¶
SetVolumeCmd sends a volume change to mpv.
func TickAfterCmd ¶ added in v0.8.2
TickAfterCmd returns a command that polls mpv state after the interval. An app uses it to wait longer while mpv has no position yet.
Types ¶
type CommandErrMsg ¶ added in v0.8.2
type CommandErrMsg struct {
Err error
}
CommandErrMsg reports that the player did not accept a new pause state, a new speed, or a new volume. The app then shows a state that the player does not have. A seek that fails needs no message: the next position tick has the position of the player.
type Group ¶ added in v0.6.0
type Group struct {
// contains filtered or unexported fields
}
Group manages independently looping mpv players as one playback unit.
type GroupControlMsg ¶ added in v0.6.0
type GroupEndedMsg ¶ added in v0.6.0
type GroupReadyMsg ¶ added in v0.6.0
type GroupStoppedMsg ¶ added in v0.6.0
type GroupStoppedMsg struct {
Group *Group
}
type Keys ¶ added in v0.6.0
type Keys struct {
PlayPause string
SeekForward string
SeekBackward string
SpeedUp string
SpeedDown string
VolumeUp string
VolumeDown string
}
Keys holds the key names for the player keybindings.
type MediaDownloadFunc ¶ added in v0.6.0
MediaDownloadFunc writes one media item and returns its byte count.
type Model ¶
type Model struct {
Playing bool
Title string
Position float64
Duration float64
Speed float64
Volume int
SleepRemaining string
RepeatStatus string
SubliminalCount int
// contains filtered or unexported fields
}
Model is the bubbletea sub-model for the player footer bar.
func (Model) PlayPauseKey ¶ added in v0.6.0
PlayPauseKey returns the play/pause key binding.
func (Model) SeekBackKey ¶
SeekBackKey returns the seek backward key binding.
func (Model) SeekForwardKey ¶
SeekForwardKey returns the seek forward key binding.
func (Model) VolumeDownKey ¶ added in v0.6.0
VolumeDownKey returns the volume-down key binding.
func (Model) VolumeUpKey ¶ added in v0.6.0
VolumeUpKey returns the volume-up key binding.
type Mpv ¶
type Mpv struct {
// ForceSeekable sets force-seekable=yes at launch, so that mpv can seek
// in an HTTP stream that it does not find seekable.
ForceSeekable bool
// contains filtered or unexported fields
}
Mpv plays one file at a time through a libmpv handle in this process.
func (*Mpv) Done ¶ added in v0.6.0
func (m *Mpv) Done() <-chan struct{}
Done returns a channel that closes when the current playback ends.
func (*Mpv) GetDuration ¶
GetDuration returns the total duration in seconds.
func (*Mpv) GetIdle ¶ added in v0.8.2
GetIdle returns whether no audio plays now. It is true for a paused player, and for a player that is not paused but waits for data, starts, or seeks.
func (*Mpv) GetPosition ¶
GetPosition returns the current playback position in seconds.
func (*Mpv) Launch ¶
Launch starts audio-only playback of url. If paused is true, mpv starts paused and the user must press play to resume. A playback that is still active is stopped first.
func (*Mpv) LaunchLoop ¶ added in v0.6.0
LaunchLoop starts playback of a local media file that loops indefinitely.
type Player ¶
type Player interface {
Launch(url, startTime string, paused bool, httpHeaders []string, volume int) error
GetPosition() (float64, error)
GetDuration() (float64, error)
GetPaused() (bool, error)
GetIdle() (bool, error)
SetPause(paused bool) error
Seek(seconds float64) error
SetSpeed(speed float64) error
SetVolume(vol int) error
Quit() error
}
Player defines the interface for media playback control.
type PlayerEndMsg ¶ added in v0.6.0
type PlayerEndMsg struct {
Generation uint64
Reason string // "eof" | "error" | "quit" | "stop" | "redirect" | "unknown"
Err error // the cause for reason "error"
}
PlayerEndMsg is emitted when the current playback ends: the file finished (eof), failed to load or decode (error), or the player stopped (quit). The app uses the Reason to decide whether to auto-advance the queue.
type PlayerKeyMap ¶
type PlayerKeyMap struct {
PlayPause key.Binding
SeekForward key.Binding
SeekBack key.Binding
SpeedUp key.Binding
SpeedDown key.Binding
VolumeUp key.Binding
VolumeDown key.Binding
}
PlayerKeyMap defines keybindings active when the player is playing.
type PlayerLaunchErrMsg ¶
PlayerLaunchErrMsg signals that mpv failed to launch.
type PlayerReadyMsg ¶
type PlayerReadyMsg struct {
Generation uint64
}
PlayerReadyMsg signals that mpv has accepted the file.
type PositionMsg ¶
type PositionMsg struct {
Position float64
Duration float64
Paused bool
// Idle is true if no audio plays now. A player that waits for data is
// idle and not paused.
Idle bool
Err error
Generation uint64 // ties this tick to a specific play session
}
PositionMsg carries the current playback position polled from mpv.
type SessionCache ¶ added in v0.6.0
type SessionCache struct {
// contains filtered or unexported fields
}
SessionCache stores media files for the lifetime of one app process. Downloads are serialized so the configured size limit cannot be exceeded by concurrent cache requests.
func NewSessionCache ¶ added in v0.6.0
func NewSessionCache(maxBytes int64) (*SessionCache, error)
NewSessionCache creates a private cache directory that mpv can access.
func (*SessionCache) Close ¶ added in v0.6.0
func (c *SessionCache) Close() error
Close removes every cached file.
func (*SessionCache) Path ¶ added in v0.6.0
func (c *SessionCache) Path(itemID string) (string, bool)
Path returns a complete cached file for itemID.
func (*SessionCache) Store ¶ added in v0.6.0
func (c *SessionCache) Store(ctx context.Context, itemID string, download MediaDownloadFunc) (string, error)
Store downloads itemID once and returns its local path.
type StartPlayMsg ¶
type StartPlayMsg struct {
Title string
}
StartPlayMsg signals that playback should begin.