class MyPlayBin(object):

	def __init__(self):
		self.__init_attributes()
		self.__arrange_widgets()
		self.__window.connect("delete-event", self.__delete_event_cb)
		self.__play_button.connect("clicked", self.__clicked_play_button_cb)
		self.__stop_button.connect("clicked", self.__clicked_stop_button_cb)
		self.__window.show_all()

	def __init_attributes(self):
		self.__bad_stream = "http://66.250.45.118:7204"
		self.__nice_stream = "http://scfire-ntc0l-2.stream.aol.com:80/stream/1074"
		from gst import element_factory_make
		self.__element = element_factory_make("playbin", "MainPipeline")
		self.__bus = self.__element.get_bus()
		self.__bus.add_signal_watch()
		self.__bus.connect("message::buffering", self.__buffering_cb)
		from gtk import Window, Label, Button
		from gtk import STOCK_MEDIA_PLAY, STOCK_MEDIA_PAUSE
		self.__label = Label("READY")
		self.__play_button = Button(stock=STOCK_MEDIA_PLAY)
		self.__stop_button = Button(stock=STOCK_MEDIA_PAUSE)
		self.__window = Window()
		return

	def __play_uri(self):
		self.__stop()
		self.__label.set_text("Preparing to play")
		from gst import STATE_PAUSED
		self.__element.set_property("uri", self.__bad_stream)
		self.__element.set_state(STATE_PAUSED)
		return False

	def __stop(self):
		from gst import STATE_NULL, STATE_READY
		self.__element.set_state(STATE_READY)
		self.__element.set_state(STATE_NULL)
		self.__label.set_text("Paused")
		return

	def __buffering_cb(self, bus, data):
		from gst import STATE_PAUSED, STATE_PLAYING
		percentage = data.structure["buffer-percent"]
		if percentage == 100:
			self.__element.set_state(STATE_PLAYING)
			self.__label.set_text("Playing")
		else:
			self.__element.set_state(STATE_PAUSED)
			message = "%s buffering..." % percentage
			self.__label.set_text(message)
		return

	def __arrange_widgets(self):
		self.__window.set_title("MyPlayBin")
		self.__window.set_border_width(10)
		from gtk import VBox, HBox
		vbox = VBox(spacing=10)
		hbox = HBox(spacing=20)
		self.__window.add(vbox)
		vbox.pack_start(self.__label, True, True, 0)
		vbox.pack_start(hbox)
		hbox.pack_start(self.__play_button, True, True, 0)
		hbox.pack_start(self.__stop_button, True, True, 0)
		return

	def __clicked_play_button_cb(self, *args):
		self.__play_uri()
		return False

	def __clicked_stop_button_cb(self, *args):
		self.__stop()
		return False

	def __delete_event_cb(self, *args):
		raise SystemExit
		return True

if __name__ == "__main__":
	from gobject import threads_init, MainLoop
	threads_init()
	MyPlayBin()
	MainLoop().run()
