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 forward_button = new Button();
- private Button rewind_button = new Button();
- private Button fullscreen_button = new Button();
- 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 int64 time = 0;
- private Label time_label = new Label("");
- 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(1000, (GLib.SourceFunc) time_set);
- }
- 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);
- // forward_button
- forward_button.set_image(new Image.from_stock (Stock.MEDIA_FORWARD, IconSize.BUTTON));
- forward_button.set_relief(Gtk.ReliefStyle.NONE);
- forward_button.tooltip_text = "Skip Forward";
- forward_button.can_focus = false;
- forward_button.clicked.connect(() => {on_seek(5);});
- // rewind_button
- rewind_button.set_image(new Image.from_stock (Stock.MEDIA_REWIND, IconSize.BUTTON));
- rewind_button.set_relief(Gtk.ReliefStyle.NONE);
- rewind_button.tooltip_text = "Rewind";
- rewind_button.can_focus = false;
- rewind_button.clicked.connect(() => {on_seek(-5);});
- // 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;
- // 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(rewind_button, true, true, 0);
- hbox.pack_start(progress_slider, true, true, 0);
- hbox.pack_start(forward_button, 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(time_label, false, true, 0);
- vbox.pack_start(hbox, false, true, 0);
- add(vbox);
- destroy.connect (on_quit);
- }
- private void on_seek(int seek)
- {
- if (state)
- {
- int64 pos = get_time();
- int64 newpos = pos + (seek * SECOND);
- playbin.seek_simple(Format.TIME, SeekFlags.FLUSH | SeekFlags.ACCURATE, newpos);
- }
- }
- private int64 get_time()
- {
- Format fmt = Format.TIME;
- int64 pos = 0;
- playbin.query_position(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 time_set()
- {
- time = get_time() / SECOND;
- time_label.set_text(time.to_string());
- }
- public 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