Class: Airstream::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/airstream/player.rb

Constant Summary collapse

SEEK_SECONDS =
30

Instance Method Summary collapse

Constructor Details

#initialize(device, files) ⇒ Player

Returns a new instance of Player.



6
7
8
9
10
11
12
13
# File 'lib/airstream/player.rb', line 6

def initialize(device, files)
  @device, @files = device, files
  unless @files.empty?
    play
  else
    @finished = true
  end
end

Instance Method Details

#current_titleObject



71
72
73
# File 'lib/airstream/player.rb', line 71

def current_title
  @files[@file_index]
end

#durationObject



87
88
89
# File 'lib/airstream/player.rb', line 87

def duration
  @device.duration
end

#elapsed_timeObject



83
84
85
# File 'lib/airstream/player.rb', line 83

def elapsed_time
  @device.position
end

#finished?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/airstream/player.rb', line 75

def finished?
  @finished || false
end

#loading?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/airstream/player.rb', line 79

def loading?
  elapsed_time == 0
end

#nextObject



53
54
55
56
57
58
59
60
# File 'lib/airstream/player.rb', line 53

def next
  @file_index += 1
  if @file_index < @files.size
    self.current_file = @files[@file_index]
  else
    @finished = true
  end
end

#play(index = 0) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/airstream/player.rb', line 15

def play(index=0)
  unless @files.fetch(index, false)
    err_msg = "no file found at index #{index}"
    raise IndexError, err_msg
  end
  @file_index = index
  self.current_file = @files[@file_index]
end

#prevObject



62
63
64
65
66
67
68
69
# File 'lib/airstream/player.rb', line 62

def prev
  if @file_index == 0
    self.seek(0)
  elsif @files.size > @file_index
    @file_index -= 1
    self.current_file = @files[@file_index]
  end
end

#seek(seconds) ⇒ Object



47
48
49
50
51
# File 'lib/airstream/player.rb', line 47

def seek(seconds)
  if seconds.between? 0, duration
    @device.scrub(seconds)
  end
end

#update(io) ⇒ Object

TODO register commands instead of switch known



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/airstream/player.rb', line 30

def update(io)
  if io.quit?
    Airstream::Io.show_input ; @finished = true
  elsif io.skip? || duration <= elapsed_time
    self.next
  elsif io.prev?
    self.prev
  elsif io.fwd?
    self.seek(elapsed_time + SEEK_SECONDS)
  elsif io.back?
    self.seek(elapsed_time - SEEK_SECONDS)
  elsif io.pause?
    @pause ||= false
    (@pause = !@pause) ? @device.pause : @device.resume
  end
end