Class: Walkman::Player

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

Constant Summary collapse

SERVICES =
[Walkman::Services::Rdio]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



12
13
14
15
16
# File 'lib/walkman/player.rb', line 12

def initialize
  @current_song = nil
  @playing = false
  @running = false
end

Instance Attribute Details

#current_songObject

Returns the value of attribute current_song.



7
8
9
# File 'lib/walkman/player.rb', line 7

def current_song
  @current_song
end

#playingObject

Returns the value of attribute playing.



7
8
9
# File 'lib/walkman/player.rb', line 7

def playing
  @playing
end

#playlistObject



92
93
94
# File 'lib/walkman/player.rb', line 92

def playlist
  @playlist ||= Walkman::Playlist.new
end

Instance Method Details

#next(count = 1) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/walkman/player.rb', line 84

def next(count = 1)
  if @current_song = playlist.next(count)
    @playing = true
  else
    stop
  end
end

#playObject



68
69
70
71
72
# File 'lib/walkman/player.rb', line 68

def play
  Walkman.logger.debug("player play")

  @playing = true
end

#servicesObject



18
19
20
21
22
# File 'lib/walkman/player.rb', line 18

def services
  @services ||= begin
    Hash[SERVICES.map { |service| [service.name, service.new] }]
  end
end

#shutdownObject



59
60
61
62
63
64
65
66
# File 'lib/walkman/player.rb', line 59

def shutdown
  @running = false
  @play_loop.join if @play_loop

  services.each do |key, service|
    service.shutdown
  end
end

#startupObject



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
# File 'lib/walkman/player.rb', line 24

def startup
  services.each do |key, service|
    service.startup
  end

  @running = true

  @play_loop = Thread.new do
    current_loop_song = nil
    last_loop_song = nil

    while @running
      if @playing
        current_loop_song = @current_song

        if current_loop_song.nil?
          self.next
        elsif !last_loop_song.nil? && current_loop_song != last_loop_song
          stop
          current_loop_song = nil
          @playing = true # have to reset this due to calling stop
        elsif last_loop_song.nil?
          play_song(current_loop_song)
        end

        last_loop_song = current_loop_song
      else
        last_loop_song = nil
      end

      sleep 0.1
    end
  end
end

#stopObject



74
75
76
77
78
79
80
81
82
# File 'lib/walkman/player.rb', line 74

def stop
  Walkman.logger.debug("player stop")

  @playing = false

  services.each do |key, service|
    service.stop
  end
end