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
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.
-
#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.
-
#next_song ⇒ void
Tell mpv to play the next song in its queue.
-
#now_playing ⇒ Song
Get mpv's currently playing 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?, #debug, #debug?, #error, #fail_arity, #info, #music?, #output, #pretty, resolve_command, resolve_method, #verbose, #verbose?, #warn, #warn_arity
Constructor Details
This class inherits a constructor from Muzak::Player::StubPlayer
Instance Method Details
#activate! ⇒ void
This method returns an undefined value.
Activate mpv by executing it and preparing for event processing.
21 22 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 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/muzak/player/mpv.rb', line 21 def activate! return if running? debug "activating #{self.class}" @sock_path = Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock") mpv_args = [ "--idle", # if i get around to separating album art from playback, # these two flags disable mpv's video output entirely # "--no-force-window", # "--no-video", "--no-osc", "--no-osd-bar", "--no-input-default-bindings", "--no-input-cursor", "--no-terminal", "--load-scripts=no", # autoload and other scripts with clobber our mpv management "--input-ipc-server=%{socket}" % { socket: @sock_path } ] mpv_args << "--geometry=#{Config.art_geometry}" if Config.art_geometry @pid = Process.spawn("mpv", *mpv_args) until File.exists?(@sock_path) sleep 0.1 end @socket = UNIXSocket.new(@sock_path) @command_queue = Queue.new @result_queue = Queue.new @event_queue = Queue.new @command_thread = Thread.new { pump_commands! } @results_thread = Thread.new { pump_results! } @events_thread = Thread.new { dispatch_events! } instance.event :player_activated end |
#clear_queue ⇒ void
This method returns an undefined value.
Clears mpv's internal queue.
180 181 182 183 184 |
# File 'lib/muzak/player/mpv.rb', line 180 def clear_queue return unless running? command "playlist-clear" end |
#deactivate! ⇒ void
This method returns an undefined value.
Deactivate mpv by killing it and cleaning up.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/muzak/player/mpv.rb', line 65 def deactivate! return unless running? debug "deactivating #{self.class}" command "quit" Process.kill :TERM, @pid Process.wait @pid @pid = nil @socket.close ensure instance.event :player_deactivated File.delete(@sock_path) if @sock_path && File.exists?(@sock_path) 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.
135 136 137 138 139 140 141 |
# File 'lib/muzak/player/mpv.rb', line 135 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.
147 148 149 150 151 152 153 |
# File 'lib/muzak/player/mpv.rb', line 147 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.
125 126 127 128 129 |
# File 'lib/muzak/player/mpv.rb', line 125 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.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/muzak/player/mpv.rb', line 158 def list_queue entries = get_property "playlist/count" playlist = [] entries.times do |i| playlist << Song.new(get_property("playlist/#{i}/filename")) end playlist 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.
110 111 112 |
# File 'lib/muzak/player/mpv.rb', line 110 def next_song command "playlist-next" end |
#now_playing ⇒ Song
Get mpv's currently playing song.
188 189 190 191 192 |
# File 'lib/muzak/player/mpv.rb', line 188 def return unless running? && Song.new(get_property "path") end |
#pause ⇒ void
Does nothing is playback is already paused.
This method returns an undefined value.
Tell mpv to pause playback.
94 95 96 97 98 |
# File 'lib/muzak/player/mpv.rb', line 94 def pause return unless running? 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.
85 86 87 88 89 |
# File 'lib/muzak/player/mpv.rb', line 85 def play return unless running? set_property "pause", false end |
#playing? ⇒ Boolean
Returns Whether or not mpv is currently playing.
101 102 103 104 105 |
# File 'lib/muzak/player/mpv.rb', line 101 def return false unless running? !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.
117 118 119 |
# File 'lib/muzak/player/mpv.rb', line 117 def previous_song command "playlist-prev" end |
#running? ⇒ Boolean
Returns Whether or not the current instance is running.
11 12 13 14 15 16 17 |
# File 'lib/muzak/player/mpv.rb', line 11 def running? begin !!@pid && Process.waitpid(@pid, Process::WNOHANG).nil? rescue Errno::ECHILD false end end |
#shuffle_queue ⇒ void
This method returns an undefined value.
Shuffle mpv's internal queue.
172 173 174 175 176 |
# File 'lib/muzak/player/mpv.rb', line 172 def shuffle_queue return unless running? command "playlist-shuffle" end |