Guest User

Untitled

a guest
Apr 22nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 5.70 KB | None | 0 0
  1. using Gtk;
  2. using Gst;
  3.  
  4. public class player_window : Window
  5. {
  6.     private DrawingArea drawing_area = new DrawingArea();
  7.     private bool state = false;
  8.     private bool fullscreened = false;
  9.     private string window_title = "My Fiwst Pwogwam";
  10.     private dynamic Element sink = ElementFactory.make ("xvimagesink", "sink");
  11.     private dynamic Element playbin = ElementFactory.make("playbin2", "playbin");
  12.     private Button open_button = new Button();
  13.     private Button play_button = new Button();
  14.     private Button fullscreen_button = new Button();
  15.     private Label position_label = new Label("");
  16.     private Scale progress_slider = new HScale.with_range(0, 1, 1);
  17.     private VBox vbox = new VBox(false, 0);
  18.     private HBox hbox = new HBox(false, 1);
  19.     private Image play_image = new Image.from_stock (Stock.MEDIA_PLAY, IconSize.BUTTON);
  20.     private Image pause_image = new Image.from_stock (Stock.MEDIA_PAUSE, IconSize.BUTTON);
  21.    
  22.     public player_window()
  23.     {
  24.         create_widgets();
  25.         Timeout.add(1000, (GLib.SourceFunc) update_slide);
  26.         Timeout.add(100, (GLib.SourceFunc) update_label);
  27.     }
  28.    
  29.     private void create_widgets()
  30.     {
  31.         title = window_title;
  32.         drawing_area.set_size_request(624, 352);
  33.  
  34.         // open_button
  35.         open_button.set_image(new Image.from_stock (Stock.OPEN, IconSize.BUTTON));
  36.         open_button.set_relief(Gtk.ReliefStyle.NONE);
  37.         open_button.tooltip_text = "Open";
  38.         open_button.can_focus = false;
  39.         open_button.clicked.connect(on_open);
  40.         // play_button
  41.         play_button.set_image(play_image);
  42.         play_button.set_relief(Gtk.ReliefStyle.NONE);
  43.         play_button.tooltip_text = "Play";
  44.         play_button.can_focus = false;
  45.         play_button.clicked.connect(on_play);
  46.         // fullscreen_button
  47.         fullscreen_button.set_image(new Image.from_stock (Stock.FULLSCREEN, IconSize.BUTTON));
  48.         fullscreen_button.set_relief(Gtk.ReliefStyle.NONE);
  49.         fullscreen_button.tooltip_text = "Fullscreen";
  50.         fullscreen_button.can_focus = false;
  51.         fullscreen_button.clicked.connect(on_fullscreen);
  52.         // progress_slider
  53.         progress_slider.can_focus = false;
  54.         progress_slider.set_draw_value (false);
  55.         progress_slider.set_size_request(380, -1);
  56.         progress_slider.margin_left = 10;
  57.         progress_slider.margin_right = 10;
  58.         progress_slider.set_range(0, 100);
  59.         progress_slider.set_increments(0, 10);
  60.         progress_slider.value_changed.connect(on_slide);
  61.        
  62.        
  63.         // conjure darkness - surely there's a simpler way
  64.         var black = Gdk.Color() {red=0,green=0,blue=0};
  65.  
  66.         // unleash darkness
  67.         drawing_area.modify_bg(Gtk.StateType.NORMAL, black);
  68.         // The following will be used after I finish implementing custom button images
  69.         // modify_bg(Gtk.StateType.NORMAL, black);
  70.  
  71.         hbox.pack_start(play_button, false, true, 0);
  72.         hbox.pack_start(position_label, false, true, 0);
  73.         hbox.pack_start(progress_slider, true, true, 0);
  74.         hbox.pack_start(fullscreen_button, false, true, 0);
  75.         hbox.pack_start(open_button, false, true, 0);
  76.         vbox.pack_start(drawing_area, true, true, 0);
  77.         vbox.pack_start(hbox, false, true, 0);
  78.         add(vbox);
  79.         destroy.connect (on_quit);
  80.     }
  81.    
  82.     private int64 get_time(int which)
  83.     {
  84.         //which = 0: Get the current position in time
  85.         //which = 1: Get the duration of the media
  86.         Format fmt = Format.TIME;
  87.         int64 pos;
  88.         if (which == 0) playbin.query_position(ref fmt, out pos);
  89.         else playbin.query_duration(ref fmt, out pos);
  90.         return pos;
  91.     }
  92.    
  93.     private void on_play()
  94.     {
  95.         if (state)
  96.         {
  97.             playbin.set_state(State.PAUSED);
  98.             state = false;
  99.             play_button.set_image(play_image);
  100.         }
  101.         else
  102.         {
  103.             playbin.set_state (State.PLAYING);
  104.             state = true;
  105.             play_button.set_image(pause_image);
  106.         }
  107.     }
  108.  
  109.     private void create_playbin(string uri)
  110.     {
  111.         playbin.set_state(State.READY);
  112.         playbin.uri = uri;
  113.         playbin.video_sink = sink;
  114.         sink.set("force-aspect-ratio", true);
  115.         ((XOverlay) sink).set_xwindow_id (Gdk.X11Window.get_xid (drawing_area.get_window ()));
  116.         title = uri;
  117.         on_play();
  118.     }
  119.  
  120.     private void on_open()
  121.     {
  122.         var file_chooser = new FileChooserDialog("Select media", this, FileChooserAction.OPEN, Stock.CANCEL, ResponseType.CANCEL, Stock.OPEN, ResponseType.ACCEPT, null);
  123.         if (file_chooser.run() == ResponseType.ACCEPT)
  124.         {
  125.             if (state) state = false;
  126.             create_playbin(file_chooser.get_uri());
  127.         }
  128.         file_chooser.destroy();
  129.     }
  130.  
  131.     private void on_fullscreen()
  132.     {
  133.         if (!fullscreened)
  134.         {
  135.             fullscreen();
  136.             fullscreened = true;
  137.         }
  138.         else
  139.         {
  140.             unfullscreen();
  141.             fullscreened = false;
  142.         }
  143.     }
  144.    
  145.     private void on_slide()
  146.     {
  147.         int64 secs = (int64) progress_slider.get_value();
  148.         playbin.seek_simple(Format.TIME, SeekFlags.FLUSH | SeekFlags.KEY_UNIT, secs * SECOND);
  149.     }
  150.    
  151.     private void update_slide()
  152.     {
  153.         var pos = get_time(0) / SECOND;
  154.         var dur = get_time(1) / SECOND;
  155.         progress_slider.value_changed.disconnect(on_slide);
  156.         progress_slider.set_range(0, dur);
  157.         progress_slider.set_value(pos);
  158.         progress_slider.value_changed.connect(on_slide);
  159.     }
  160.    
  161.     private void update_label()
  162.     {
  163.         int min = 0;
  164.         int64 secs = get_time(0) / SECOND;
  165.         string seconds;
  166.         while (secs >= 60)
  167.         {
  168.             ++min;
  169.             secs -= 60;
  170.         }
  171.         if (secs < 10) seconds = "0" + secs.to_string();
  172.         else seconds = secs.to_string();
  173.         position_label.set_text(min.to_string() + ":" + seconds);
  174.     }
  175.    
  176.     private void on_quit()
  177.     {
  178.         // Avoids memory issues
  179.         playbin.set_state (State.NULL);
  180.         Gtk.main_quit ();
  181.     }
  182.  
  183. }
  184.  
  185. public static int main(string[] args)
  186. {
  187.     Gtk.init(ref args);
  188.     Gst.init(ref args);
  189.     var app = new player_window();
  190.     app.show_all();
  191.     Gtk.main();
  192.     return 0;
  193. }
Add Comment
Please, Sign In to add comment