Module: VLC::Client::MediaControls

Included in:
VLC::Client
Defined in:
lib/vlc-client/client/media_controls.rb

Constant Summary collapse

STATUS_MAPPING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Expressions for parsing VLC's "status" command

{
  file: /\( new input: file:\/\/(.*) \)/,
  volume: /\( audio volume: (\d+) \)/,
  state: /\( state (.*) \)/ ,
}.freeze

Instance Method Summary collapse

Instance Method Details

#lengthInteger

Gets the length of the media being played

Returns:

  • (Integer)

    time in seconds


70
71
72
73
74
# File 'lib/vlc-client/client/media_controls.rb', line 70

def length
  Integer(connection.write("get_length", false))
rescue ArgumentError
  0
end

#pauseObject

Pauses playback


37
38
39
# File 'lib/vlc-client/client/media_controls.rb', line 37

def pause
  connection.write("pause")
end

#play(media) ⇒ Object #playObject

Plays media or resumes playback

Overloads:

  • #play(media) ⇒ Object

    addes the given media and plays it

    Examples:

    vlc.play('http://example.org/media.mp3')

    Parameters:

    • media (String, File, URI)

      the media to be played

  • #playObject

    plays the current media or resume playback is paused

    Examples:

    vlc.play('http://example.org/media.mp3')
    vlc.pause
    vlc.play #resume playback

32
33
34
# File 'lib/vlc-client/client/media_controls.rb', line 32

def play(media = nil)
  connection.write(media.nil? ? "play" : "add #{media(media)}")
end

#playing?Boolean

Queries VLC if media is being played

Returns:

  • (Boolean)

86
87
88
# File 'lib/vlc-client/client/media_controls.rb', line 86

def playing?
  connection.write("is_playing", false) == "1"
end

#progressInteger

Get the progress of the the media being played

Returns:

  • (Integer)

    a relative value on percentage


80
81
82
83
# File 'lib/vlc-client/client/media_controls.rb', line 80

def progress
  l = length
  l.zero? ? 0 : 100 * time / l
end

#seek(seconds = 0) ⇒ Object

Seek in seconds


42
43
44
# File 'lib/vlc-client/client/media_controls.rb', line 42

def seek(seconds = 0)
  connection.write("seek #{seconds.to_i}")
end

#statusHash{Symbol => String}

Returns the mapping of status strings.

Examples:

status = vlc.status
status[:file] # => "/path/to/file/playing.mp3"
status[:volume] # => "256"
status[:state] # => "playing"

Returns:

  • (Hash{Symbol => String})

    the mapping of status strings


123
124
125
126
127
128
129
130
# File 'lib/vlc-client/client/media_controls.rb', line 123

def status
  connection.write("status")
  raw_status = 3.times.collect { connection.read }

  STATUS_MAPPING.keys.zip(raw_status).map do |k, s|
    [k, STATUS_MAPPING[k].match(s)[1]]
  end.to_h
end

#stopObject

Stops media currently playing


47
48
49
# File 'lib/vlc-client/client/media_controls.rb', line 47

def stop
  connection.write("stop")
end

#stopped?Boolean

Queries VLC if playback is currently stopped

Returns:

  • (Boolean)

91
92
93
# File 'lib/vlc-client/client/media_controls.rb', line 91

def stopped?
  connection.write("is_playing", false) == "0"
end

#timeInteger

Gets the current playback progress in time

Returns:

  • (Integer)

    time in seconds


60
61
62
63
64
# File 'lib/vlc-client/client/media_controls.rb', line 60

def time
  Integer(connection.write("get_time", false))
rescue ArgumentError
  0
end

#titleObject

Gets the title of the media at play


52
53
54
# File 'lib/vlc-client/client/media_controls.rb', line 52

def title
  connection.write("get_title", false)
end

#volumeInteger #volume(level) ⇒ Object

Queries/Sets VLC volume level

Overloads:

  • #volumeInteger

    Returns the current volume level.

    Returns:

    • (Integer)

      the current volume level

  • #volume(level) ⇒ Object

    Parameters:

    • level (Integer)

      the volume level to set


105
106
107
108
109
110
# File 'lib/vlc-client/client/media_controls.rb', line 105

def volume(level = nil)
  return Integer(connection.write("volume", false)) if level.nil?
  connection.write("volume #{Integer(level)}")
rescue ArgumentError
  level.nil? ? 0 : nil
end

#volume=(level) ⇒ Object

See Also:


113
114
115
# File 'lib/vlc-client/client/media_controls.rb', line 113

def volume=(level)
  volume(level)
end