mpris

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 4 Imported by: 1

README

GO-MPRIS

A Go library for reading data from players and controlling them using MPRIS.

Install

$ go get github.com/Endg4meZer0/go-mpris

The dependency github.com/godbus/dbus/v5 will be installed as well.

Example

Printing the current playback status and then changing it:

import (
	"log"

	"github.com/Endg4meZer0/go-mpris"
	"github.com/godbus/dbus/v5"
)

func main() {
	conn, err := dbus.SessionBus()
	if err != nil {
		panic(err)
	}
	names, err := mpris.List(conn)
	if err != nil {
		panic(err)
	}
	if len(names) == 0 {
		log.Fatal("No players found")
	}

	name := names[0]
	player := mpris.New(conn, name)

	status, err := player.GetPlaybackStatus()
	if err != nil {
		log.Fatalf("Could not get current playback status: %s", err)
	}

	log.Printf("The player was %s...", status)
	err = player.PlayPause()
	if err != nil {
		log.Fatalf("Could not play/pause player: %s", err)
	}
}

For more examples, see the examples folder.

Go Docs

Read the docs at https://pkg.go.dev/github.com/Endg4meZer0/go-mpris.

Credits

Pauloo27 for the original code.

emersion for the original-original code.

leberKleber and their code for several additional ideas regarding metadata.

Documentation

Index

Constants

View Source
const (
	BaseInterface      = "org.mpris.MediaPlayer2"
	PlayerInterface    = "org.mpris.MediaPlayer2.Player"
	TrackListInterface = "org.mpris.MediaPlayer2.TrackList"
	PlaylistsInterface = "org.mpris.MediaPlayer2.Playlists"
)

DBus consts

Variables

This section is empty.

Functions

func List

func List(conn *dbus.Conn) ([]string, error)

Lists the available players in alphabetical order.

func RegisterNameOwnerChanged added in v1.0.3

func RegisterNameOwnerChanged(conn *dbus.Conn, ch chan<- *dbus.Signal) (err error)

func UnregisterNameOwnerChanged added in v1.0.3

func UnregisterNameOwnerChanged(conn *dbus.Conn, ch chan<- *dbus.Signal) (err error)

Types

type LoopStatus

type LoopStatus string

The status of the player loop. May be "None", "Track" or "Playlist".

const (
	LoopNone     LoopStatus = "None"
	LoopTrack    LoopStatus = "Track"
	LoopPlaylist LoopStatus = "Playlist"
)

Loop statuses

type Metadata

type Metadata map[string]dbus.Variant

A representation of MPRIS metadata. See also: https://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata

func (Metadata) Album

func (md Metadata) Album() (string, error)

Returns the album name.

func (Metadata) AlbumArtist

func (md Metadata) AlbumArtist() ([]string, error)

Returns the album artist(s)

func (Metadata) ArtURL

func (md Metadata) ArtURL() (string, error)

Returns the location of an image representing the track or album.

func (Metadata) Artist

func (md Metadata) Artist() ([]string, error)

Returns the track artist(s).

func (Metadata) AsText

func (md Metadata) AsText() (string, error)

Returns the track lyrics.

func (Metadata) AudioBPM

func (md Metadata) AudioBPM() (int, error)

Returns the speed of the music in beats per minute.

func (Metadata) AutoRating

func (md Metadata) AutoRating() (float64, error)

Returns an automatically-generated rating, based on things such as how often it has been played. This should be in the range 0.0 to 1.0.

func (Metadata) Comment

func (md Metadata) Comment() ([]string, error)

Comment returns a (list of) freeform comment(s).

func (Metadata) Composer

func (md Metadata) Composer() ([]string, error)

Returns the composer(s) of the track.

func (Metadata) ContentCreated

func (md Metadata) ContentCreated() (time.Time, error)

Returns when the track was created.

func (Metadata) DiscNumber

func (md Metadata) DiscNumber() (int, error)

Returns the disc number on the album that this track is from.

func (Metadata) Find

func (md Metadata) Find(key string) (dbus.Variant, bool)

Find returns a generic representation of the requested value when present.

func (Metadata) FirstUsed

func (md Metadata) FirstUsed() (time.Time, error)

Returns when the track was first played.

func (Metadata) Genre

func (md Metadata) Genre() ([]string, error)

Returns the genre(s) of the track.

func (Metadata) LastUsed

func (md Metadata) LastUsed() (time.Time, error)

Returns when the track was last played.

func (Metadata) Length

func (md Metadata) Length() (int64, error)

Returns the duration of the track in microseconds. Why int64 and not uint64: https://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata/#mpris:length

func (Metadata) Lyricist

func (md Metadata) Lyricist() ([]string, error)

Returns the lyricist(s) of the track.

func (Metadata) Title

func (md Metadata) Title() (string, error)

Returns the track title.

func (Metadata) TrackID

func (md Metadata) TrackID() (dbus.ObjectPath, error)

Returns a unique identity for the track within the context of an MPRIS object (e.g. tracklist).

func (Metadata) TrackNumber

func (md Metadata) TrackNumber() (int, error)

TrackNumber returns the track number on the album disc.

func (Metadata) URL

func (md Metadata) URL() (string, error)

Returns the location of the media file.

func (Metadata) UseCount

func (md Metadata) UseCount() (int, error)

Returns the number of times the track has been played.

func (Metadata) UserRating

func (md Metadata) UserRating() (float64, error)

UserRating returns a user-specified rating. This should be in the range 0.0 to 1.0.

type PlaybackStatus

type PlaybackStatus string

The status of the playback. May be "Playing", "Paused" or "Stopped".

const (
	PlaybackPlaying PlaybackStatus = "Playing"
	PlaybackPaused  PlaybackStatus = "Paused"
	PlaybackStopped PlaybackStatus = "Stopped"
)

Playback statuses

type Player

type Player struct {
	// contains filtered or unexported fields
}

A representation of a MPRIS player.

func New

func New(conn *dbus.Conn, name string) *Player

Connects to the player with the specified name using the specified DBus connection.

func (*Player) AddTrack

func (i *Player) AddTrack(uri string, afterTrack string, setAsCurrent bool) error

Adds the specified Uri after a specified track and if it should become current track. See also: https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Method:AddTrack

func (*Player) CanControl

func (i *Player) CanControl() (bool, error)

Returns if the player can be controlled by calls. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:CanControl

func (*Player) CanEditTracks

func (i *Player) CanEditTracks() (bool, error)

Returns if the track list can be edited by calls. See also: https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Property:CanEditTracks

func (*Player) CanGoNext

func (i *Player) CanGoNext() (bool, error)

Returns if the player can switch to the next track using the Next call. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:CanGoNext

func (*Player) CanGoPrevious

func (i *Player) CanGoPrevious() (bool, error)

Returns if the player can switch to the previous track using the Previous call. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:CanGoPrevious

func (*Player) CanPause

func (i *Player) CanPause() (bool, error)

Returns if the player can be paused by Pause or PlayPause calls. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:CanPause

func (*Player) CanPlay

func (i *Player) CanPlay() (bool, error)

Returns if the player can be started by Play or PlayPause calls. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:CanPlay

func (*Player) CanQuit

func (i *Player) CanQuit() (bool, error)

Returns the CanQuit property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:CanQuit

func (*Player) CanRaise

func (i *Player) CanRaise() (bool, error)

Returns the CanRaise property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:CanRaise

func (*Player) CanSeek

func (i *Player) CanSeek() (bool, error)

Returns if the position can be controlled by Seek and SetPosition calls. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:CanSeek

func (*Player) CanSetFullscreen

func (i *Player) CanSetFullscreen() (bool, error)

Added in MPRIS v2.2. Returns the CanSetFullscreen property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:CanSetFullscreen

func (*Player) GetDesktopEntry

func (i *Player) GetDesktopEntry() (string, error)

Returns the DesktopEntry property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:DesktopEntry

func (*Player) GetFullscreen

func (i *Player) GetFullscreen() (bool, error)

Added in MPRIS v2.2. Returns the Fullscreen property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:Fullscreen

func (*Player) GetIdentity

func (i *Player) GetIdentity() (string, error)

Returns the Identity property, which is the friendly name of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:Identity

func (*Player) GetMaximumRate

func (i *Player) GetMaximumRate() (float64, error)

Returns the maximum value that Rate can take. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:MaximumRate

func (*Player) GetMinimumRate

func (i *Player) GetMinimumRate() (float64, error)

Returns the minimum value that Rate can take. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:MinimumRate

func (*Player) GetName

func (i *Player) GetName() string

Gets the player full name (including base interface name).

func (*Player) GetPosition

func (i *Player) GetPosition() (int64, error)

Returns the currently playing track's position in microseconds. If there isn't any, returns 0. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:Position

func (*Player) GetRate

func (i *Player) GetRate() (float64, error)

Returns the current playback rate. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:Rate

func (*Player) GetShortName

func (i *Player) GetShortName() string

Gets the player short name (without the base interface name).

func (*Player) GetShuffle

func (i *Player) GetShuffle() (bool, error)

Returns if shuffle is enabled or not. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:Shuffle

func (*Player) GetSupportedMimeTypes

func (i *Player) GetSupportedMimeTypes() ([]string, error)

Returns the SupportedMimeTypes property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:SupportedMimeTypes

func (*Player) GetSupportedUriSchemes

func (i *Player) GetSupportedUriSchemes() ([]string, error)

Returns the SupportedUriSchemes property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:SupportedUriSchemes

func (*Player) GetTracks

func (i *Player) GetTracks() ([]string, error)

Returns the track IDs of the current track list. See also: https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Property:Tracks

func (*Player) GetTracksMetadata

func (i *Player) GetTracksMetadata(tracks []string) ([]Metadata, error)

Returns the specified tracks' metadata. See also: https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Method:GetTracksMetadata

func (*Player) GoTo

func (i *Player) GoTo(trackId string) error

Goes to the specified track in the track list. See also: https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Method:GoTo

func (*Player) HasTrackList

func (i *Player) HasTrackList() (bool, error)

Returns the HasTrackList property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:HasTrackList

func (*Player) Next

func (i *Player) Next() error

Skips to the next track in the tracklist. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:Next

func (*Player) OpenUri

func (i *Player) OpenUri(uri string) error

Opens the Uri for a playback, if supported. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:OpenUri

func (*Player) Play

func (i *Player) Play() error

Starts or resumes playback. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:Play

func (*Player) PlayPause

func (i *Player) PlayPause() error

Resumes playback if paused and pauses playback if playing. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:PlayPause

func (*Player) Previous

func (i *Player) Previous() error

Skips to the previous track in the tracklist. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:Previous

func (*Player) Raise

func (i *Player) Raise() error

Raises player priority. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Method:Raise

func (*Player) RegisterSignalReceiver added in v1.0.2

func (i *Player) RegisterSignalReceiver(ch chan<- *dbus.Signal) (err error)

Registers a new signal receiver channel that will be able to get signals as specified in SignalType definition.

func (*Player) RemoveTrack

func (i *Player) RemoveTrack(trackId string) error

Removes the specified track from the track list. See also: https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Method:RemoveTrack

func (*Player) SeekBy

func (i *Player) SeekBy(offset int64) error

Seeks in the current track position by the specified offset. The offset should be in microseconds. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:Seek

func (*Player) SetFullscreen

func (i *Player) SetFullscreen(value bool) error

Added in MPRIS v2.2. Sets the Fullscreen property of the player. See also: https://specifications.freedesktop.org/mpris-spec/latest/Media_Player.html#Property:Fullscreen

func (*Player) SetLoopStatus

func (i *Player) SetLoopStatus(loopStatus LoopStatus) error

Sets the loop status to the specified value. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:LoopStatus

func (*Player) SetPosition

func (i *Player) SetPosition(position int64) error

Sets the currently playing track's position in microseconds (if there is any). Not to confuse with SetTrackPosition. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:Position

func (*Player) SetTrackPosition

func (i *Player) SetTrackPosition(trackId dbus.ObjectPath, position int64) error

Sets the specified track's position in microseconds (if it's playing). Perhaps you would like to use SetPosition instead. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Method:SetPosition

func (*Player) SetVolume

func (i *Player) SetVolume(value float64) error

Sets the volume to the specified value. See also: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:Volume

func (*Player) UnregisterSignalReceiver added in v1.0.3

func (i *Player) UnregisterSignalReceiver(ch chan *dbus.Signal) (err error)

type SignalType added in v1.0.2

type SignalType string

The type of signal received by channel. May be "PropertiesChanged", "NameOwnerChanged" and "Seeked"

const (
	SignalNotSupported         SignalType = ""
	SignalPropertiesChanged    SignalType = "PropertiesChanged"
	SignalNameOwnerChanged     SignalType = "NameOwnerChanged"
	SignalSeeked               SignalType = "Seeked"
	SignalTrackListReplaced    SignalType = "TrackListReplaced"
	SignalTrackAdded           SignalType = "TrackAdded"
	SignalTrackRemoved         SignalType = "TrackRemoved"
	SignalTrackMetadataChanged SignalType = "TrackMetadataChanged"
)

Signal types

func GetSignalType added in v1.0.2

func GetSignalType(signal *dbus.Signal) SignalType

Gets the supported signal type from *dbus.Signal

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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