### Full mpvipc Usage Example Source: https://pkg.go.dev/github.com/DexterLB/mpvipc This example demonstrates connecting to an mpv IPC socket, retrieving the current file path, pausing playback, observing volume changes, and handling events. It requires mpv to be running with an IPC socket. Ensure the socket path matches the one mpv is configured with. ```go package main import ( "fmt" "log" "github.com/DexterLB/mpvipc" ) func main() { conn := mpvipc.NewConnection("/tmp/mpv_rpc") err := conn.Open() if err != nil { log.Fatal(err) } defer conn.Close() events, stopListening := conn.NewEventListener() path, err := conn.Get("path") if err != nil { log.Fatal(err) } log.Printf("current file playing: %s", path) err = conn.Set("pause", true) if err != nil { log.Fatal(err) } log.Printf("paused!") _, err = conn.Call("observe_property", 42, "volume") if err != nil { fmt.Print(err) } go func() { conn.WaitUntilClosed() stopListening <- struct{}{} }() for event := range events { if event.ID == 42 { log.Printf("volume now is %f", event.Data.(float64)) } else { log.Printf("received event: %s", event.Name) } } log.Printf("mpv closed socket") } ``` -------------------------------- ### Open Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Connects to the MPV socket. Returns an error if already connected. It also starts listening to events, so ListenForEvents() can be called afterwards. ```APIDOC ## func (*Connection) Open ### Description Open connects to the socket. Returns an error if already connected. It also starts listening to events, so ListenForEvents() can be called afterwards. ### Signature ```go func (c *Connection) Open() error ``` ``` -------------------------------- ### Run mpv with IPC socket Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Before using the mpvipc package, ensure mpv is running with the IPC socket enabled. This command starts mpv playing a file and exposes its IPC interface at the specified socket path. ```bash $ mpv some_file.mkv --input-unix-socket=/tmp/mpv_socket ``` -------------------------------- ### Open MPV IPC Connection Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Connects to the MPV IPC socket. Returns an error if already connected. It also starts listening to events. ```go func (c *Connection) Open() error ``` -------------------------------- ### Get a specific mpv property Source: https://pkg.go.dev/github.com/DexterLB/mpvipc A convenience method for retrieving the value of a specific mpv property. It is equivalent to calling Call("get_property", property). ```go func (c *Connection) Get(property string) (interface{}, error) ``` -------------------------------- ### Connection.Open Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Establishes the connection to the mpv IPC socket. This must be called before any other operations on the connection. ```APIDOC ## Connection.Open ### Description Establishes the connection to the mpv IPC socket. This must be called before any other operations on the connection. ### Signature ```go func (c *Connection) Open() error ``` ### Returns * **error** - An error if the connection could not be opened. ``` -------------------------------- ### Set MPV Property Source: https://pkg.go.dev/github.com/DexterLB/mpvipc A shortcut to Call("set_property", property, value) for setting MPV properties. ```go func (c *Connection) Set(property string, value interface{}) error ``` -------------------------------- ### Create a new mpv event listener Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Returns a channel for receiving mpv events and a channel to signal the listener to stop. This is a convenient way to manage event listening. ```go func (c *Connection) NewEventListener() (chan *Event, chan struct{}) ``` -------------------------------- ### Create a new mpv IPC connection Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Initializes a new Connection object for a given socket name. The connection is not established until Open() is called. ```go func NewConnection(socketName string) *Connection ``` -------------------------------- ### Set Source: https://pkg.go.dev/github.com/DexterLB/mpvipc A shortcut to Call("set_property", property, value) for setting MPV properties. ```APIDOC ## func (*Connection) Set ### Description Set is a shortcut to Call("set_property", property, value) ### Signature ```go func (c *Connection) Set(property string, value interface{}) error ``` ``` -------------------------------- ### Open the mpv IPC connection Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Establishes the connection to the mpv IPC socket. This must be called before any other communication methods. ```go func (c *Connection) Open() error ``` -------------------------------- ### Create Event Listener Channel Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Creates and returns an event channel and a stop channel for listening to MPV events. Read events from the events channel and send an empty struct to the stop channel to close it. ```go func (c *Connection) NewEventListener() (chan *Event, chan struct{}) ``` -------------------------------- ### Call an arbitrary mpv IPC command Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Executes any mpv command and returns its result. Refer to mpv's documentation for a list of available commands. ```go func (c *Connection) Call(arguments ...interface{}) (interface{}, error) ``` -------------------------------- ### NewEventListener Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Creates and returns an event channel and a stop channel for listening to MPV events. Read events from the events channel and send an empty struct to the stop channel to close it. ```APIDOC ## func (*Connection) NewEventListener ### Description NewEventListener is a convenience wrapper around ListenForEvents(). It creates and returns the event channel and the stop channel. After calling NewEventListener, read events from the events channel and send an empty struct to the stop channel to close it. ### Signature ```go func (c *Connection) NewEventListener() (chan *Event, chan struct{}) ``` ``` -------------------------------- ### Connection.Call Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Calls an arbitrary mpv command with the given arguments and returns its result. Refer to the mpv manual for a list of available commands. ```APIDOC ## Connection.Call ### Description Calls an arbitrary mpv command with the given arguments and returns its result. Refer to the mpv manual for a list of available commands. ### Signature ```go func (c *Connection) Call(arguments ...interface{}) (interface{}, error) ``` ### Parameters * **arguments** (...interface{}) - The command and its arguments. ### Returns * **interface{}** - The result of the command. * **error** - An error if the command failed. ``` -------------------------------- ### MPV Hub Structure Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Maintains the set of active EventListeners and broadcasts messages to them. ```go type Hub struct { // contains filtered or unexported fields } ``` -------------------------------- ### Connection.Get Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Retrieves the value of a specific mpv property. This is a shortcut for calling Call("get_property", property). ```APIDOC ## Connection.Get ### Description Retrieves the value of a specific mpv property. This is a shortcut for calling Call("get_property", property). ### Signature ```go func (c *Connection) Get(property string) (interface{}, error) ``` ### Parameters * **property** (string) - The name of the property to retrieve. ### Returns * **interface{}** - The value of the property. * **error** - An error if the property could not be retrieved. ``` -------------------------------- ### Connection.NewEventListener Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Creates a new event listener. It returns a channel for receiving events and a channel to signal the listener to stop. ```APIDOC ## Connection.NewEventListener ### Description Creates a new event listener. It returns a channel for receiving events and a channel to signal the listener to stop. ### Signature ```go func (c *Connection) NewEventListener() (chan *Event, chan struct{}) ``` ### Returns * **chan *Event** - A channel that receives mpv events. * **chan struct{}** - A channel that can be used to stop the event listener. ``` -------------------------------- ### MPV Event Structure Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Represents an event received from MPV. Contains fields like Name, Reason, Prefix, Level, Text, ID, Data, and ExtraData. ```go type Event struct { // Name is the only obligatory field: the name of the event Name string `json:"event"` // Reason is the reason for the event: currently used for the "end-file" // event. When Name is "end-file", possible values of Reason are: // "eof", "stop", "quit", "error", "redirect", "unknown" Reason string `json:"reason"` // Prefix is the log-message prefix (only if Name is "log-message") Prefix string `json:"prefix"` // Level is the loglevel for a log-message (only if Name is "log-message") Level string `json:"level"` // Text is the text of a log-message (only if Name is "log-message") Text string `json:"text"` // ID is the user-set property ID (on events triggered by observed properties) ID int64 `json:"id"` // Data is the property value (on events triggered by observed properties) Data interface{} `json:"data"` // ExtraData contains extra attributes of the event payload (on spontaneous events) ExtraData map[string]interface{} `json:"-"` } ``` -------------------------------- ### MPV EventListener Structure Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Represents an event listener within the MPV IPC system. Contains filtered or unexported fields. ```go type EventListener struct { // contains filtered or unexported fields } ``` -------------------------------- ### Set a specific mpv property Source: https://pkg.go.dev/github.com/DexterLB/mpvipc A convenience method for setting the value of a specific mpv property. It is equivalent to calling Call("set_property", property, value). ```go func (c *Connection) Set(property string, value interface{}) error ``` -------------------------------- ### NewConnection Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Creates a new Connection object associated with the given Unix socket name. This does not establish the connection itself. ```APIDOC ## NewConnection ### Description Creates a new Connection object associated with the given Unix socket name. ### Signature ```go func NewConnection(socketName string) *Connection ``` ### Parameters * **socketName** (string) - The name of the Unix domain socket to connect to. ``` -------------------------------- ### Listen for mpv events Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Continuously receives events from mpv and sends them to the provided channel until a signal is received on the stop channel. The events channel is closed upon method return. ```go func (c *Connection) ListenForEvents(events chan<- *Event, stop <-chan struct{}) ``` -------------------------------- ### Connection.Set Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Sets the value of a specific mpv property. This is a shortcut for calling Call("set_property", property, value). ```APIDOC ## Connection.Set ### Description Sets the value of a specific mpv property. This is a shortcut for calling Call("set_property", property, value). ### Signature ```go func (c *Connection) Set(property string, value interface{}) error ``` ### Parameters * **property** (string) - The name of the property to set. * **value** (interface{}) - The value to set the property to. ### Returns * **error** - An error if the property could not be set. ``` -------------------------------- ### WaitUntilClosed Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Blocks until the MPV connection becomes closed. See IsClosed() for an explanation of the closed state. ```APIDOC ## func (*Connection) WaitUntilClosed ### Description WaitUntilClosed blocks until the connection becomes closed. See IsClosed() for an explanation of the closed state. ### Signature ```go func (c *Connection) WaitUntilClosed() ``` ``` -------------------------------- ### Hub Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Maintains the set of active EventListeners and broadcasts messages to them. ```APIDOC ## type Hub ### Description Hub maintains the set of active EventListeners and broadcasts messages to them. ``` -------------------------------- ### Connection struct definition Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Represents an active connection to an mpv IPC socket. ```go type Connection struct { // contains filtered or unexported fields } ``` -------------------------------- ### Wait for Connection Closure Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Blocks until the MPV IPC connection becomes closed. See IsClosed() for an explanation of the closed state. ```go func (c *Connection) WaitUntilClosed() ``` -------------------------------- ### Event Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Represents an event received from MPV. For a list of all possible events, see https://mpv.io/manual/master/#list-of-events ```APIDOC ## type Event ### Description Event represents an event received from mpv. For a list of all possible events, see https://mpv.io/manual/master/#list-of-events ### Fields - **Name** (string) - The name of the event (obligatory field). - **Reason** (string) - The reason for the event (currently used for the "end-file" event). When Name is "end-file", possible values of Reason are: "eof", "stop", "quit", "error", "redirect", "unknown". - **Prefix** (string) - The log-message prefix (only if Name is "log-message"). - **Level** (string) - The loglevel for a log-message (only if Name is "log-message"). - **Text** (string) - The text of a log-message (only if Name is "log-message"). - **ID** (int64) - The user-set property ID (on events triggered by observed properties). - **Data** (interface{}) - The property value (on events triggered by observed properties). - **ExtraData** (map[string]interface{}) - Contains extra attributes of the event payload (on spontaneous events). ``` -------------------------------- ### Event struct definition Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Represents an event received from mpv. ```go type Event ``` -------------------------------- ### Connection.WaitUntilClosed Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Blocks until the connection to mpv is closed. This can happen if Close() is called, mpv exits, or an error occurs. ```APIDOC ## Connection.WaitUntilClosed ### Description Blocks until the connection to mpv is closed. This can happen if Close() is called, mpv exits, or an error occurs. ### Signature ```go func (c *Connection) WaitUntilClosed() ``` ``` -------------------------------- ### Unmarshal JSON data into an Event Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Custom JSON unmarshaling for the Event type. ```go func (e *Event) UnmarshalJSON(data []byte) error ``` -------------------------------- ### Connection.ListenForEvents Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Blocks until something is received on the stop channel (or it's closed). In the mean time, events received on the socket will be sent on the events channel. They may not appear in the same order they happened in. The events channel is closed automatically just before this method returns. ```APIDOC ## Connection.ListenForEvents ### Description Blocks until something is received on the stop channel (or it's closed). In the mean time, events received on the socket will be sent on the events channel. They may not appear in the same order they happened in. The events channel is closed automatically just before this method returns. ### Signature ```go func (c *Connection) ListenForEvents(events chan<- *Event, stop <-chan struct{}) ``` ### Parameters * **events** (chan<- *Event) - The channel to send received events on. * **stop** (<-chan struct{}) - The channel to signal the listener to stop. ``` -------------------------------- ### UnmarshalJSON Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Custom JSON unmarshaling for the Event type. ```APIDOC ## func (*Event) UnmarshalJSON ### Description Custom JSON unmarshaling for the Event type. ### Signature ```go func (e *Event) UnmarshalJSON(data []byte) error ``` ``` -------------------------------- ### Check if the mpv IPC connection is closed Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Returns true if the connection is closed. This can happen due to explicit closing, failure to open, or mpv exiting. ```go func (c *Connection) IsClosed() bool ``` -------------------------------- ### Unmarshal JSON into MPV Event Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Unmarshals JSON data into an MPV Event struct. ```go func (e *Event) UnmarshalJSON(data []byte) error ``` -------------------------------- ### Connection.IsClosed Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Returns true if the connection is closed. A connection is considered closed if Close() has been called, Open() hasn't been called yet, or if the connection terminated due to an error or mpv exiting. ```APIDOC ## Connection.IsClosed ### Description Returns true if the connection is closed. A connection is considered closed if Close() has been called, Open() hasn't been called yet, or if the connection terminated due to an error or mpv exiting. ### Signature ```go func (c *Connection) IsClosed() bool ``` ### Returns * **bool** - True if the connection is closed, false otherwise. ``` -------------------------------- ### Wait until the mpv IPC connection is closed Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Blocks execution until the mpv IPC connection is closed. This is useful for keeping a listener active until mpv exits or the connection is otherwise terminated. ```go func (c *Connection) WaitUntilClosed() ``` -------------------------------- ### Connection.Close Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Closes the socket, disconnecting from mpv. It is safe to call Close() on an already closed connection. ```APIDOC ## Connection.Close ### Description Closes the socket, disconnecting from mpv. It is safe to call Close() on an already closed connection. ### Signature ```go func (c *Connection) Close() error ``` ### Returns * **error** - An error if the connection could not be closed. ``` -------------------------------- ### Close the mpv IPC connection Source: https://pkg.go.dev/github.com/DexterLB/mpvipc Gracefully disconnects from the mpv IPC socket. This operation is safe to call even if the connection is already closed. ```go func (c *Connection) Close() error ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.