Class: Tucue::Player
- Inherits:
-
Object
- Object
- Tucue::Player
- Defined in:
- lib/tucue/player.rb
Overview
Playback engine that controls mpv over –input-ipc-server.
Spawns mpv as a child process and talks to it through a Unix domain socket, sending JSON IPC commands to seek and read the playback position. mpv.io/manual/stable/#json-ipc
Defined Under Namespace
Classes: Error
Constant Summary collapse
- SOCKET_TIMEOUT =
Max seconds to wait for the IPC socket to appear.
5
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#duration ⇒ Object
Total duration in seconds, or nil if unavailable.
-
#initialize(file, socket_path: "/tmp/tucue.sock", start_at: nil, extra_args: []) ⇒ Player
constructor
A new instance of Player.
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #play ⇒ Object
-
#seek(seconds) ⇒ Object
Seek relative to the current position (seconds; negative rewinds).
-
#start ⇒ Object
Launch mpv and connect to its IPC socket.
-
#stop ⇒ Object
Quit mpv and close the socket.
-
#time_pos ⇒ Object
Current playback position in seconds, or nil if unavailable.
-
#toggle_pause ⇒ Object
Toggle between playing and paused.
Constructor Details
#initialize(file, socket_path: "/tmp/tucue.sock", start_at: nil, extra_args: []) ⇒ Player
Returns a new instance of Player.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tucue/player.rb', line 19 def initialize(file, socket_path: "/tmp/tucue.sock", start_at: nil, extra_args: []) @file = file @socket_path = socket_path @start_at = start_at @extra_args = extra_args @pid = nil @socket = nil @request_id = 0 @mutex = Mutex.new end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
30 31 32 |
# File 'lib/tucue/player.rb', line 30 def file @file end |
Instance Method Details
#duration ⇒ Object
Total duration in seconds, or nil if unavailable.
80 81 82 |
# File 'lib/tucue/player.rb', line 80 def duration get_property("duration") end |
#pause ⇒ Object
61 62 63 |
# File 'lib/tucue/player.rb', line 61 def pause set_property("pause", true) end |
#paused? ⇒ Boolean
65 66 67 |
# File 'lib/tucue/player.rb', line 65 def paused? get_property("pause") == true end |
#play ⇒ Object
57 58 59 |
# File 'lib/tucue/player.rb', line 57 def play set_property("pause", false) end |
#seek(seconds) ⇒ Object
Seek relative to the current position (seconds; negative rewinds).
70 71 72 |
# File 'lib/tucue/player.rb', line 70 def seek(seconds) command("seek", seconds, "relative") end |
#start ⇒ Object
Launch mpv and connect to its IPC socket.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/tucue/player.rb', line 33 def start raise Error, "mpv not found in PATH" unless mpv_available? File.unlink(@socket_path) if File.exist?(@socket_path) @pid = spawn( "mpv", "--no-video", "--no-terminal", "--input-ipc-server=#{@socket_path}", *(@start_at ? ["--start=#{@start_at}"] : []), *@extra_args, @file ) connect self end |
#stop ⇒ Object
Quit mpv and close the socket.
85 86 87 88 89 90 91 |
# File 'lib/tucue/player.rb', line 85 def stop command("quit") if @socket rescue Error # Already gone; ignore. ensure close end |
#time_pos ⇒ Object
Current playback position in seconds, or nil if unavailable.
75 76 77 |
# File 'lib/tucue/player.rb', line 75 def time_pos get_property("time-pos") end |
#toggle_pause ⇒ Object
Toggle between playing and paused.
53 54 55 |
# File 'lib/tucue/player.rb', line 53 def toggle_pause command("cycle", "pause") end |