#38 Better Overnight DJ (radio_player2.rb)
This script, radio_player2.rb
, is an improvement on radio_player1.rb
. Instead of placeholder Procs, it will actually play sound files, as well as log playback with specific times.
The Code
#a/usr/bin/env ruby
# radio_player2.rb
❶ LOG_FILE = '/tmp/radio_player2.log'
❷ PLAYERS = {
'.mp3' => 'mpg321',
'.ogg' => 'ogg123',
'' => 'ls'
}
❸ # these are variables, local to Kernel.
# They work just as well as constants.
play_file_proc = lambda do |filename| Callbacks
❹ ext = File.extname(filename)
❺ system("#{PLAYERS[ext]} #{filename}") if PLAYERS[ext]
❻ File.open(LOG_FILE, 'a') do |log|
log.puts([Time.now, filename].join("\t") + "\n")
end
end
dont_play_file_proc = lambda do |filename| puts "I'm not playing #{filename}. ...
Get Ruby by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.