Class: Muzak::Player::MPV
- Inherits:
-
StubPlayer
- Object
- StubPlayer
- Muzak::Player::MPV
- Defined in:
- lib/muzak/player/mpv.rb
Overview
Exposes MPV's IPC to muzak for playback control.
Instance Attribute Summary
Attributes inherited from StubPlayer
Class Method Summary collapse
-
.available? ⇒ Boolean
Whether or not MPV is available for execution.
Instance Method Summary collapse
-
#activate! ⇒ void
Activate mpv by executing it and preparing for event processing.
-
#clear_queue ⇒ void
Clears mpv's internal queue.
-
#deactivate! ⇒ void
Deactivate mpv by killing it and cleaning up.
-
#dispatch_event!(event) ⇒ void
private
Dispatch the given event to the active Instance.
-
#enqueue_album(album) ⇒ void
Tell mpv to add the given album to its queue.
-
#enqueue_playlist(playlist) ⇒ void
Tell mpv to add the given playlist to its queue.
-
#enqueue_song(song) ⇒ void
Tell mpv to add the given song to its queue.
-
#list_queue ⇒ Array<Song>
Get mpv's internal queue.
-
#load_song(song, art) ⇒ void
private
Load a song and optional album art into mpv.
-
#next_song ⇒ void
Tell mpv to play the next song in its queue.
-
#now_playing ⇒ Song?
Get mpv's currently loaded song.
-
#pause ⇒ void
Tell mpv to pause playback.
-
#play ⇒ void
Tell mpv to begin playback.
-
#playing? ⇒ Boolean
Whether or not mpv is currently playing.
-
#previous_song ⇒ void
Tell mpv to play the previous song in its queue.
-
#running? ⇒ Boolean
Whether or not the current instance is running.
-
#shuffle_queue ⇒ void
Shuffle mpv's internal queue.
Methods inherited from StubPlayer
Methods included from Utils
album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?
Constructor Details
This class inherits a constructor from Muzak::Player::StubPlayer
Class Method Details
Instance Method Details
#activate! ⇒ void
This method returns an undefined value.
Activate mpv by executing it and preparing for event processing.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/muzak/player/mpv.rb', line 23 def activate! return if running? debug "activating #{self.class}" mpv_args = [ # if i get around to separating album art from playback, # these two flags disable mpv's video output entirely # "--no-force-window", # "--no-video", # there's also this, which (might) also work # "--audio-display=no", "--no-osc", "--no-osd-bar", "--no-input-default-bindings", "--no-input-cursor", "--load-scripts=no", # autoload and other scripts with clobber our mpv management ] mpv_args << "--geometry=#{Config.art_geometry}" if Config.art_geometry @mpv = ::MPV::Session.new(user_args: mpv_args) @mpv.callbacks << ::MPV::Callback.new(self, :dispatch_event!) instance.event :player_activated end |
#clear_queue ⇒ void
This method returns an undefined value.
Clears mpv's internal queue.
164 165 166 167 168 |
# File 'lib/muzak/player/mpv.rb', line 164 def clear_queue return unless running? @mpv.command "playlist-clear" end |
#deactivate! ⇒ void
This method returns an undefined value.
Deactivate mpv by killing it and cleaning up.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/muzak/player/mpv.rb', line 52 def deactivate! return unless running? debug "deactivating #{self.class}" @mpv.quit! ensure @_now_playing = nil instance.event :player_deactivated end |
#dispatch_event!(event) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Dispatch the given event to the active Instance.
191 192 193 194 195 196 197 198 199 |
# File 'lib/muzak/player/mpv.rb', line 191 def dispatch_event!(event) case event when "file-loaded" instance.event :song_loaded, when "end-file" instance.event :song_unloaded @_now_playing = nil end end |
#enqueue_album(album) ⇒ void
Activates mpv if not already activated.
This method returns an undefined value.
Tell mpv to add the given album to its queue.
116 117 118 119 120 121 122 |
# File 'lib/muzak/player/mpv.rb', line 116 def enqueue_album(album) activate! unless running? album.songs.each do |song| load_song song, album.cover_art end end |
#enqueue_playlist(playlist) ⇒ void
Activates mpv if not already activated.
This method returns an undefined value.
Tell mpv to add the given playlist to its queue.
128 129 130 131 132 133 134 |
# File 'lib/muzak/player/mpv.rb', line 128 def enqueue_playlist(playlist) activate! unless running? playlist.songs.each do |song| load_song song, song.best_guess_album_art end end |
#enqueue_song(song) ⇒ void
Activates mpv if not already activated.
This method returns an undefined value.
Tell mpv to add the given song to its queue.
106 107 108 109 110 |
# File 'lib/muzak/player/mpv.rb', line 106 def enqueue_song(song) activate! unless running? load_song song, song.best_guess_album_art end |
#list_queue ⇒ Array<Song>
This includes songs already played.
Get mpv's internal queue.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/muzak/player/mpv.rb', line 139 def list_queue entries = @mpv.get_property "playlist/count" playlist = [] entries.times do |i| # TODO: this is slow and should be avoided at all costs, # since we have access to these Song instances earlier # in the object's lifecycle. playlist << Song.new(@mpv.get_property("playlist/#{i}/filename")) end playlist end |
#load_song(song, art) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Load a song and optional album art into mpv.
181 182 183 184 185 |
# File 'lib/muzak/player/mpv.rb', line 181 def load_song(song, art) cmds = ["loadfile", song.path, "append-play"] cmds << "external-file=\"#{art}\"" if art @mpv.command *cmds end |
#next_song ⇒ void
Does nothing if the current song is the last.
This method returns an undefined value.
Tell mpv to play the next song in its queue.
91 92 93 |
# File 'lib/muzak/player/mpv.rb', line 91 def next_song @mpv.command "playlist-next" end |
#now_playing ⇒ Song?
Get mpv's currently loaded song.
172 173 174 |
# File 'lib/muzak/player/mpv.rb', line 172 def @_now_playing ||= Song.new(@mpv.get_property "path") end |
#pause ⇒ void
Does nothing is playback is already paused.
This method returns an undefined value.
Tell mpv to pause playback.
75 76 77 78 79 |
# File 'lib/muzak/player/mpv.rb', line 75 def pause return unless running? @mpv.set_property "pause", true end |
#play ⇒ void
Does nothing is playback is already in progress.
This method returns an undefined value.
Tell mpv to begin playback.
66 67 68 69 70 |
# File 'lib/muzak/player/mpv.rb', line 66 def play return unless running? @mpv.set_property "pause", false end |
#playing? ⇒ Boolean
Returns Whether or not mpv is currently playing.
82 83 84 85 86 |
# File 'lib/muzak/player/mpv.rb', line 82 def return false unless running? !@mpv.get_property "pause" end |
#previous_song ⇒ void
Does nothing if the current song is the first.
This method returns an undefined value.
Tell mpv to play the previous song in its queue.
98 99 100 |
# File 'lib/muzak/player/mpv.rb', line 98 def previous_song @mpv.command "playlist-prev" end |
#running? ⇒ Boolean
Returns Whether or not the current instance is running.
17 18 19 |
# File 'lib/muzak/player/mpv.rb', line 17 def running? @mpv&.running? end |
#shuffle_queue ⇒ void
This method returns an undefined value.
Shuffle mpv's internal queue.
156 157 158 159 160 |
# File 'lib/muzak/player/mpv.rb', line 156 def shuffle_queue return unless running? @mpv.command "playlist-shuffle" end |