Class: Plllayer::SinglePlayers::Nop

Inherits:
Plllayer::SinglePlayer show all
Defined in:
lib/plllayer/single_players/nop.rb

Overview

This is the SinglePlayer implentation used for testing. It doesn’t actually do anything, it just pretends to play a music file using sleeps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNop

Returns a new instance of Nop.



8
9
10
11
12
13
14
# File 'lib/plllayer/single_players/nop.rb', line 8

def initialize
  @paused = false
  @muted = false
  @volume = 50
  @speed = 1.0
  @track_path = nil
end

Instance Attribute Details

#track_pathObject (readonly)

Returns the value of attribute track_path.



6
7
8
# File 'lib/plllayer/single_players/nop.rb', line 6

def track_path
  @track_path
end

Instance Method Details

#muteObject



108
109
110
# File 'lib/plllayer/single_players/nop.rb', line 108

def mute
  @muted = true
end

#muted?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/plllayer/single_players/nop.rb', line 104

def muted?
  @muted
end

#pauseObject



62
63
64
65
66
67
68
# File 'lib/plllayer/single_players/nop.rb', line 62

def pause
  if not @paused and playing?
    @paused = true
  else
    false
  end
end

#paused?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/plllayer/single_players/nop.rb', line 58

def paused?
  @paused
end

#play(track_path, &on_end) ⇒ Object

Raises:



20
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
# File 'lib/plllayer/single_players/nop.rb', line 20

def play(track_path, &on_end)
  _quit

  # Make sure the audio file exists.
  raise FileNotFoundError, "file '#{track_path}' doesn't exist" unless File.exists? track_path

  @paused = false
  @track_path = track_path

  # Assume the filename starts with the song length in milliseconds, so we
  # don't actually have to read the file.
  @total_time = File.basename(@track_path, ".*").to_i
  @time_left = @total_time
  @last_tick = Time.now

  @quit_hook_active = false
  @quit_hook = Thread.new do
    while @time_left > 0
      unless @paused
        @time_left -= ((Time.now - @last_tick) * 1000 * @speed).to_i
      end
      @last_tick = Time.now
      sleep 0.01
    end
    @quit_hook_active = true
    @paused = false
    @track_path = nil
    @started = nil
    on_end.call
  end

  true
end

#playing?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/plllayer/single_players/nop.rb', line 16

def playing?
  not @track_path.nil?
end

#positionObject



127
128
129
# File 'lib/plllayer/single_players/nop.rb', line 127

def position
  playing? ? @total_time - @time_left : false
end

#resumeObject



70
71
72
73
74
75
76
77
# File 'lib/plllayer/single_players/nop.rb', line 70

def resume
  if @paused
    @paused = false
    true
  else
    false
  end
end

#seek(where, type = :absolute) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/plllayer/single_players/nop.rb', line 79

def seek(where, type = :absolute)
  if playing?
    case type
    when :absolute
      @time_left = @total_time - where
    when :relative
      @time_left -= where
    when :percent
      @time_left = @total_time - (@total_time * where) / 100
    end
    true
  else
    false
  end
end

#speedObject



95
96
97
# File 'lib/plllayer/single_players/nop.rb', line 95

def speed
  @speed
end

#speed=(new_speed) ⇒ Object



99
100
101
102
# File 'lib/plllayer/single_players/nop.rb', line 99

def speed=(new_speed)
  @speed = new_speed.to_f
  true
end

#stopObject



54
55
56
# File 'lib/plllayer/single_players/nop.rb', line 54

def stop
  _quit
end

#track_lengthObject



131
132
133
# File 'lib/plllayer/single_players/nop.rb', line 131

def track_length
  playing? ? @total_time : false
end

#unmuteObject



112
113
114
115
# File 'lib/plllayer/single_players/nop.rb', line 112

def unmute
  @muted = false
  true
end

#volumeObject



117
118
119
# File 'lib/plllayer/single_players/nop.rb', line 117

def volume
  @volume
end

#volume=(new_volume) ⇒ Object



121
122
123
124
125
# File 'lib/plllayer/single_players/nop.rb', line 121

def volume=(new_volume)
  @muted = false
  @volume = new_volume.to_f
  true
end