Guest User

Untitled

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