Class: WillYouStartTheFansPlease::BackgroundAudio

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackgroundAudio

Returns a new instance of BackgroundAudio.



9
10
11
12
13
14
15
16
# File 'lib/will_you_start_the_fans_please/background_audio.rb', line 9

def initialize
  @loop_file = File.expand_path("../../../audio/loop.m4a", __FILE__)
  @ending_file = File.expand_path("../../../audio/ending.m4a", __FILE__)

  # puts 'playing audio...'
  # puts @loop_file
  self.start_playback
end

Instance Attribute Details

#ending_file=(value) ⇒ Object (writeonly)

Sets the attribute ending_file

Parameters:

  • value

    the value to set the attribute ending_file to.



5
6
7
# File 'lib/will_you_start_the_fans_please/background_audio.rb', line 5

def ending_file=(value)
  @ending_file = value
end

#loop_file=(value) ⇒ Object (writeonly)

Sets the attribute loop_file

Parameters:

  • value

    the value to set the attribute loop_file to.



5
6
7
# File 'lib/will_you_start_the_fans_please/background_audio.rb', line 5

def loop_file=(value)
  @loop_file = value
end

Instance Method Details

#start_playbackObject



18
19
20
# File 'lib/will_you_start_the_fans_please/background_audio.rb', line 18

def start_playback
  @stdin, @stdout, @stderr, @thread = Open3.popen3("while :; do afplay '" + @loop_file + "' ; done")
end

#stop_playbackObject



22
23
24
25
26
27
28
29
30
# File 'lib/will_you_start_the_fans_please/background_audio.rb', line 22

def stop_playback
  # puts 'stopping audio...'
  # puts @ending_file
  parent_thread = @thread.pid
  child_threads = `pgrep -P #{@thread.pid}`.split("\n").to_a
  Process.kill "INT", parent_thread
  child_threads.each{|pid| Process.kill "INT", pid.to_i }
  system("afplay '" + @ending_file + "'")
end