Play a file using ruby


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. require 'rubygems'
  2.  
  3. class Player
  4. @@sound_path = ''
  5. @@sound_app = 'mpg321'
  6. @@process_devnull = '> /dev/null 2>&1'
  7. @@process_bg = '&'
  8.  
  9. def self.sound_path=(value)
  10. @@sound_path = value
  11. end
  12.  
  13. def self.sound_app=(value)
  14. @@sound_app = value
  15. end
  16.  
  17. def self.process_devnull=(value)
  18. @@process_devnull = value
  19. end
  20.  
  21. def self.process_bg=(value)
  22. @@process_bg = value
  23. end
  24.  
  25. def self.play(file)
  26. cmd = "#{@@sound_app} #{@@sound_path + file} #{@@process_devnull} #{@@process_bg}"
  27. system cmd
  28. end
  29. end
  30.  
  31. files = Dir.glob(File.join("/home/buildmaster/Desktop/sounds/error", "*.mp3"))
  32. Player.play(files[rand(files.size)])

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.