public
Description: Unlike my 'experiments' repository, these guys are deadly serious!
Homepage:
Clone URL: git://github.com/jonleighton/scripts.git
scripts / click_to_play.rb
100755 66 lines (53 sloc) 1.417 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
 
# This is a hack to facilitate my use of a bluetooth mouse as a remote input device to trigger the
# playing of an sound file.
#
# Run it by typing:
#
# ./click_to_play path_to_sound_file
#
# The window will become full screen and contain a button which fills the screen. When the button
# is pressed, the sound will play.
#
# To quit, hit the Esc key.
 
require "rubygems"
require "gtk2"
require "gst"
 
class ClickToPlay
  def initialize(file)
    raise ArgumentError, "File to play not specified" if file.nil?
    @file = file
    
    new_playbin
    build_window
  end
  
  def quit
    @playbin.stop
    Gtk.main_quit
  end
  
  def build_window
    @window = Gtk::Window.new
    @window.add_events(Gdk::Event::BUTTON_PRESS_MASK)
    
    @window.signal_connect("destroy") { quit }
    @window.signal_connect("key_press_event") do |widget, event|
      quit if event.keyval == Gdk::Keyval::GDK_Escape
    end
    @window.signal_connect("button_press_event") { play }
    
    @window.fullscreen
    @window.show_all
  end
  
  def new_playbin
    @playbin.stop if @playbin
    
    @playbin = Gst::ElementFactory.make('playbin')
    @playbin.uri = "file://#{@file}"
    @playbin.bus.add_watch do |bus, message|
      new_playbin if message.type == Gst::Message::Type::EOS
      true
    end
  end
  
  def play
  @playbin.play
  end
end
 
ClickToPlay.new(ARGV.first)
Gst.init
Gtk.main