Guest User

Untitled

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