Class: MikePlayer::Player

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

Constant Summary collapse

PLAY_SLEEP =
0.5
PAUSE_SLEEP =
1.0
STOPPED =
:stopped
PLAYING =
:playing
PAUSED =
:paused
SONG_CHANGE =
:song_change

Instance Method Summary collapse

Constructor Details

#initialize(options, *args) ⇒ Player

Returns a new instance of Player.



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
# File 'lib/mikeplayer.rb', line 22

def initialize(options, *args)
  @settings  = Settings.new(options)
  @playlist  = Playlist.new(@settings.playlist)
  @minutes   = @settings.minutes
  @command   = ''
  @timer_start = Time.now if (@minutes > 0)
  @state     = STOPPED
  @player    = PlayThread.new(volume: @settings.volume)
  @song_mutex = Mutex.new

  if (true == @settings.list?)
    @songs.map { |song| File.basename(song) }.sort.each {|song| puts "#{File.basename(song)}"}

    exit 0
  end

  args.flatten.each do |arg|
    @playlist.find_song(arg, @settings.music_dir)
  end

  if (true == @settings.random?)
    @playlist.add_random(@settings.random, @settings.music_dir)
  end

  @playlist.save
end

Instance Method Details

#playObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mikeplayer.rb', line 49

def play
  @playlist.shuffle! if @settings.shuffle?

  if @settings.fullscreen?
    Display.enter_fullscreen
    at_exit { Display.exit_fullscreen }
  end

  puts Display.colorize("Mike Player v#{MikePlayer::VERSION}", Display::BANNER_COLOR)
  puts "#{Display.colorize("Playlist #{@playlist.info}", Display::DIM_COLOR)}\n"

  if (0 == @playlist.size)
    puts "No songs in playlist."
    
    exit 1
  end

  @thread = Thread.new do
    @display = Display.new(fullscreen: @settings.fullscreen?)
    @song_i  = 0

    while (@song_i < @playlist.size)
      @display.song_info = @playlist.song_info(@song_i)

      @player.play(song.filename)

      @state   = PLAYING

      while @player.playing?
        pause_if_over_time_limit

        @display.elapsed = @player.elapsed if playing?

        @display.display!(song.length_str(@player.elapsed), minutes_remaining)

        sleep(sleep_time)
      end

      if @player.paused?
        @display.display!(song.length_str(@player.elapsed), minutes_remaining)

        while paused?
          sleep(sleep_time)
        end
      elsif playing?
        next_song
      end
    end

    @player.stop
    print("\r\n")
    exit
  end

  wait_on_user

  print("\r\n")
end