player

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package player plays audio through libmpv and provides the player model.

Index

Constants

View Source
const DefaultMediaCacheLimit int64 = 2 << 30

DefaultMediaCacheLimit bounds temporary media storage to 2 GiB.

View Source
const MaxSubliminalTracks = 8

MaxSubliminalTracks prevents accidental unbounded mpv player creation.

Variables

View Source
var ErrClosed = errors.New("player closed")

ErrClosed reports that the player is gone: no file plays, or playback ended.

View Source
var ErrMediaCacheFull = errors.New("media cache size limit reached")

ErrMediaCacheFull reports that a download would exceed the session limit.

View Source
var ErrPropertyUnavailable = errors.New("property unavailable")

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, duration float64, paused bool, httpHeaders []string, volume int, generation uint64) tea.Cmd

LaunchCmd starts playback. If paused is true, mpv starts paused. httpHeaders are the HTTP request headers of the stream. duration is the current track length in seconds; it is used to start at a percent offset when resuming into a long HTTP stream. Pass 0 to start at startTime in seconds. 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 LaunchGroupCmd(ctx context.Context, group *Group, paths []string, volume int, generation uint64) tea.Cmd

func MpvSocketDir

func MpvSocketDir() (string, error)

MpvSocketDir returns the private runtime directory of the app: /tmp/<app name>-runtime-<uid> with 0700 permissions. The media cache is in this directory.

func QuitCmd

func QuitCmd(p Player) tea.Cmd

QuitCmd stops mpv playback.

func QuitGroupCmd added in v0.6.0

func QuitGroupCmd(group *Group) tea.Cmd

func SeekCmd added in v0.6.0

func SeekCmd(p Player, target float64) tea.Cmd

SeekCmd sends an absolute seek request to mpv by target seconds.

func SeekRelativeCmd added in v0.6.0

func SeekRelativeCmd(p Player, offset float64) tea.Cmd

SeekRelativeCmd sends a relative seek request to mpv by offset seconds.

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 SetGroupPauseCmd(group *Group, paused bool, generation uint64) tea.Cmd

func SetGroupVolumeCmd added in v0.6.0

func SetGroupVolumeCmd(group *Group, volume int, generation uint64) tea.Cmd

func SetPauseCmd added in v0.6.0

func SetPauseCmd(p Player, paused bool) tea.Cmd

SetPauseCmd sets mpv's paused state.

func SetSpeedCmd

func SetSpeedCmd(p Player, speed float64) tea.Cmd

SetSpeedCmd sends a speed change to mpv.

func SetVolumeCmd

func SetVolumeCmd(p Player, vol int) tea.Cmd

SetVolumeCmd sends a volume change to mpv.

func TickCmd

func TickCmd(p Player, generation uint64) tea.Cmd

TickCmd returns a command that fires twice per second and polls mpv state. The generation parameter ties the tick to a specific play session so stale ticks from a previous session can be safely ignored.

func WatchGroupCmd added in v0.6.0

func WatchGroupCmd(ctx context.Context, group *Group, generation uint64) tea.Cmd

Types

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.

func NewGroup added in v0.6.0

func NewGroup() *Group

func (*Group) Launch added in v0.6.0

func (g *Group) Launch(ctx context.Context, paths []string, volume int) error

Launch starts every path paused, then resumes the complete group together.

func (*Group) Quit added in v0.6.0

func (g *Group) Quit() error

func (*Group) SetPause added in v0.6.0

func (g *Group) SetPause(paused bool) error

func (*Group) SetVolume added in v0.6.0

func (g *Group) SetVolume(volume int) error

func (*Group) Wait added in v0.6.0

func (g *Group) Wait(ctx context.Context) error

Wait returns when the playback of a group member ends or ctx is cancelled.

type GroupControlMsg added in v0.6.0

type GroupControlMsg struct {
	Generation uint64
	Err        error
}

type GroupEndedMsg added in v0.6.0

type GroupEndedMsg struct {
	Generation uint64
	Err        error
}

type GroupReadyMsg added in v0.6.0

type GroupReadyMsg struct {
	Generation uint64
	Err        error
}

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

type MediaDownloadFunc func(context.Context, io.Writer) (int64, error)

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 NewModel

func NewModel(p Player, speed float64, keys Keys, styles ui.Styles) Model

NewModel creates a new player sub-model with the initial speed and the key names.

func (Model) HandlesKey

func (m Model) HandlesKey(msg tea.KeyMsg) bool

HandlesKey reports whether the player should consume the key while a session is active.

func (Model) Init

func (m Model) Init() tea.Cmd

Init returns nil; the player is inactive until playback starts.

func (Model) PlayPauseKey added in v0.6.0

func (m Model) PlayPauseKey() key.Binding

PlayPauseKey returns the play/pause key binding.

func (Model) SeekBackKey

func (m Model) SeekBackKey() key.Binding

SeekBackKey returns the seek backward key binding.

func (Model) SeekForwardKey

func (m Model) SeekForwardKey() key.Binding

SeekForwardKey returns the seek forward key binding.

func (*Model) SetWidth

func (m *Model) SetWidth(w int)

SetWidth sets the available width for rendering.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update handles messages for the player sub-model.

func (Model) View

func (m Model) View() string

View renders the player footer bar. Returns empty string if inactive.

func (Model) VolumeDownKey added in v0.6.0

func (m Model) VolumeDownKey() key.Binding

VolumeDownKey returns the volume-down key binding.

func (Model) VolumeUpKey added in v0.6.0

func (m Model) VolumeUpKey() key.Binding

VolumeUpKey returns the volume-up key binding.

type Mpv

type Mpv struct {

	// ForceSeekable sets force-seekable=yes at launch, so that mpv can start
	// a long HTTP stream at a percent offset.
	ForceSeekable bool
	// contains filtered or unexported fields
}

Mpv plays one file at a time through a libmpv handle in this process.

func NewMpv

func NewMpv() *Mpv

NewMpv creates a player. libmpv loads on the first Launch.

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

func (m *Mpv) GetDuration() (float64, error)

GetDuration returns the total duration in seconds.

func (*Mpv) GetPaused

func (m *Mpv) GetPaused() (bool, error)

GetPaused returns whether playback is paused.

func (*Mpv) GetPosition

func (m *Mpv) GetPosition() (float64, error)

GetPosition returns the current playback position in seconds.

func (*Mpv) GetVolume

func (m *Mpv) GetVolume() (int, error)

GetVolume returns the current volume level.

func (*Mpv) Launch

func (m *Mpv) Launch(url, startTime string, paused bool, httpHeaders []string, volume int) error

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

func (m *Mpv) LaunchLoop(path string, paused bool, volume int) error

LaunchLoop starts playback of a local media file that loops indefinitely.

func (*Mpv) Quit

func (m *Mpv) Quit() error

Quit stops playback and destroys the mpv handle.

func (*Mpv) Seek

func (m *Mpv) Seek(seconds float64) error

Seek seeks to an absolute position in seconds.

func (*Mpv) SeekRelative added in v0.6.0

func (m *Mpv) SeekRelative(seconds float64) error

SeekRelative seeks by a relative offset in seconds.

func (*Mpv) SetPause

func (m *Mpv) SetPause(paused bool) error

SetPause pauses or resumes playback.

func (*Mpv) SetSpeed

func (m *Mpv) SetSpeed(speed float64) error

SetSpeed sets the playback speed multiplier.

func (*Mpv) SetVolume

func (m *Mpv) SetVolume(vol int) error

SetVolume sets the playback volume (0-150).

func (*Mpv) WatchEvents added in v0.6.0

func (m *Mpv) WatchEvents(generation uint64) tea.Cmd

WatchEvents returns a PlayerEndMsg when the current playback ends: at the end of the file (eof), on a load or decode error (error), or when the player stops (quit).

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)
	SetPause(paused bool) error
	Seek(seconds float64) error
	SeekRelative(seconds float64) error
	SetSpeed(speed float64) error
	SetVolume(vol int) error
	GetVolume() (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
}

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

type PlayerLaunchErrMsg struct {
	Generation uint64
	Err        error
}

PlayerLaunchErrMsg signals that mpv failed to launch.

type PlayerQuitMsg

type PlayerQuitMsg struct{}

PlayerQuitMsg signals that mpv has been quit.

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
	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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL