Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Gtk;
- using Gst;
- public class player_window : Window
- {
- private DrawingArea drawing_area = new DrawingArea();
- private bool state = false;
- private string window_title = "My Fiwst Pwogwam";
- private dynamic Element sink = ElementFactory.make ("xvimagesink", "sink");
- private dynamic Element playbin = ElementFactory.make("playbin2", "playbin");
- private Button open_button = new Button();
- private Button play_button = new Button();
- private Button fullscreen_button = new Button();
- private Label position_label = new Label("");
- private Scale progress_slider = new HScale.with_range(0, 1, 1);
- private VBox vbox = new VBox(false, 0);
- private HBox hbox = new HBox(false, 1);
- private Image play_image = new Image.from_stock (Stock.MEDIA_PLAY, IconSize.BUTTON);
- private Image pause_image = new Image.from_stock (Stock.MEDIA_PAUSE, IconSize.BUTTON);
- public player_window()
- {
- create_widgets();
- Timeout.add(100, (GLib.SourceFunc) update_slide);
- Timeout.add(100, (GLib.SourceFunc) update_label);
- }
- private void create_widgets()
- {
- title = window_title;
- drawing_area.set_size_request(624, 352);
- // open_button
- open_button.set_image(new Image.from_stock (Stock.OPEN, IconSize.BUTTON));
- open_button.set_relief(Gtk.ReliefStyle.NONE);
- open_button.tooltip_text = "Open";
- open_button.can_focus = false;
- open_button.clicked.connect(on_open);
- // play_button
- play_button.set_image(play_image);
- play_button.set_relief(Gtk.ReliefStyle.NONE);
- play_button.tooltip_text = "Play";
- play_button.can_focus = false;
- play_button.clicked.connect(on_play);
- // fullscreen_button
- fullscreen_button.set_image(new Image.from_stock (Stock.FULLSCREEN, IconSize.BUTTON));
- fullscreen_button.set_relief(Gtk.ReliefStyle.NONE);
- fullscreen_button.tooltip_text = "Fullscreen";
- fullscreen_button.can_focus = false;
- fullscreen_button.clicked.connect(on_fullscreen);
- // progress_slider
- progress_slider.can_focus = false;
- progress_slider.set_draw_value (false);
- progress_slider.set_size_request(380, -1);
- progress_slider.margin_left = 10;
- progress_slider.margin_right = 10;
- progress_slider.set_range(0, 100);
- progress_slider.set_increments(0, 10);
- progress_slider.value_changed.connect(on_slide);
- // conjure darkness - surely there's a simpler way
- var black = Gdk.Color() {red=0,green=0,blue=0};
- // unleash darkness
- drawing_area.modify_bg(Gtk.StateType.NORMAL, black);
- // The following will be used after I finish implementing custom button images
- // modify_bg(Gtk.StateType.NORMAL, black);
- hbox.pack_start(play_button, true, true, 0);
- hbox.pack_start(position_label, true, true, 0);
- hbox.pack_start(progress_slider, true, true, 0);
- hbox.pack_start(fullscreen_button, true, true, 0);
- hbox.pack_start(open_button, true, true, 0);
- vbox.pack_start(drawing_area, true, true, 0);
- vbox.pack_start(hbox, false, true, 0);
- add(vbox);
- destroy.connect (on_quit);
- }
- private int64 get_time(int which)
- {
- //which = 0: Get the current position in time
- //which = 1: Get the duration of the media
- Format fmt = Format.TIME;
- int64 pos;
- if (which == 0) playbin.query_position(ref fmt, out pos);
- else playbin.query_duration(ref fmt, out pos);
- return pos;
- }
- private void on_play()
- {
- if (state)
- {
- playbin.set_state(State.PAUSED);
- state = false;
- play_button.set_image(play_image);
- }
- else
- {
- playbin.set_state (State.PLAYING);
- state = true;
- play_button.set_image(pause_image);
- }
- }
- private void create_playbin(string uri)
- {
- playbin.set_state(State.READY);
- playbin.uri = uri;
- playbin.video_sink = sink;
- sink.set("force-aspect-ratio", true);
- ((XOverlay) sink).set_xwindow_id (Gdk.X11Window.get_xid (drawing_area.get_window ()));
- title = uri;
- on_play();
- }
- private void on_open()
- {
- var file_chooser = new FileChooserDialog("Select media", this, FileChooserAction.OPEN, Stock.CANCEL, ResponseType.CANCEL, Stock.OPEN, ResponseType.ACCEPT, null);
- if (file_chooser.run() == ResponseType.ACCEPT)
- {
- if (state) state = false;
- create_playbin(file_chooser.get_uri());
- }
- file_chooser.destroy();
- }
- private void on_fullscreen()
- {
- stdout.printf("Fullscreen not yet implemented");
- }
- private void on_slide()
- {
- int64 secs = (int64) progress_slider.get_value();
- playbin.seek_simple(Format.TIME, SeekFlags.FLUSH | SeekFlags.KEY_UNIT, secs * SECOND);
- }
- private void update_slide()
- {
- var pos = get_time(0) / SECOND;
- var dur = get_time(1) / SECOND;
- progress_slider.value_changed.disconnect(on_slide);
- progress_slider.set_range(0, dur);
- progress_slider.set_value(pos);
- progress_slider.value_changed.connect(on_slide);
- }
- private void update_label()
- {
- int min = 0;
- int64 secs = get_time(0) / SECOND;
- string seconds;
- while (secs >= 60)
- {
- ++min;
- secs -= 60;
- }
- if (secs < 10) seconds = "0" + secs.to_string();
- else seconds = secs.to_string();
- position_label.set_text(min.to_string() + ":" + seconds);
- }
- private void on_quit()
- {
- // Avoids memory issues
- playbin.set_state (State.NULL);
- Gtk.main_quit ();
- }
- }
- public static int main(string[] args)
- {
- Gtk.init(ref args);
- Gst.init(ref args);
- var app = new player_window();
- app.show_all();
- Gtk.main();
- return 0;
- }
Add Comment
Please, Sign In to add comment