Guest User

Untitled

a guest
Apr 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.66 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 Element sink = ElementFactory.make ("xvimagesink", "sink");
  9.     private dynamic Element playbin = ElementFactory.make("playbin2", "playbin");
  10.    
  11.     public player_window()
  12.     {
  13.         create_widgets();
  14.     }
  15.    
  16.     public void create_widgets()
  17.     {
  18.         title = "My Fiwst Pwogwam";
  19.         ToolButton open_button = new ToolButton.from_stock (Stock.OPEN);
  20.         ToolButton play_button = new ToolButton.from_stock (Stock.MEDIA_PLAY);
  21.         ToolButton forward_button = new ToolButton.from_stock (Stock.MEDIA_FORWARD);
  22.         ToolButton rewind_button = new ToolButton.from_stock (Stock.MEDIA_REWIND);
  23.         Box vbox = new Box(Orientation.VERTICAL, 3);
  24.         Box hbox = new Box(Orientation.HORIZONTAL, 2);
  25.         Box seekbox = new Box(Orientation.HORIZONTAL, 2);
  26.         drawing_area.set_size_request(320, 240);
  27.         hbox.pack_start(open_button, true, true, 0);
  28.         hbox.pack_start(play_button, true, true, 0);
  29.         seekbox.pack_start(rewind_button, true, true, 0);
  30.         seekbox.pack_start(forward_button, true, true, 0);
  31.         vbox.pack_start(drawing_area, true, true, 0);
  32.         vbox.pack_start(seekbox, false, true, 0);
  33.         vbox.pack_start(hbox, false, true, 0);
  34.         add(vbox);
  35.         play_button.clicked.connect(on_play);
  36.         open_button.clicked.connect(on_open);
  37.         rewind_button.clicked.connect(() => {on_seek(-5);});
  38.         forward_button.clicked.connect(() => {on_seek(5);});
  39.         destroy.connect (Gtk.main_quit);
  40.     }
  41.    
  42.     public void on_seek(int seek)
  43.     {
  44.         if (state)
  45.         {
  46.             Format fmt = Format.TIME;
  47.             int64 pos = 0;
  48.             playbin.query_position(ref fmt, out pos);
  49.             playbin.seek_simple(Format.TIME, SeekFlags.FLUSH | SeekFlags.ACCURATE, (pos + (seek * SECOND)));
  50.         }
  51.     }
  52.    
  53.     public void on_play()
  54.     {
  55.         if (state)
  56.         {
  57.             playbin.set_state(State.PAUSED);
  58.             state = false;
  59.         }
  60.         else
  61.         {
  62.             playbin.set_state (State.PLAYING);
  63.             state = true;
  64.         }
  65.     }
  66.  
  67.     public void create_playbin(string uri)
  68.     {
  69.         playbin.set_state(State.READY);
  70.         playbin.uri = uri;
  71.         playbin.video_sink = sink;
  72.         sink.set("force-aspect-ratio", true);
  73.         ((XOverlay) sink).set_xwindow_id (Gdk.X11Window.get_xid (drawing_area.get_window ()));
  74.     }
  75.  
  76.     public void on_open()
  77.     {
  78.         var file_chooser = new FileChooserDialog("Select media", this, FileChooserAction.OPEN, Stock.CANCEL, ResponseType.CANCEL, Stock.OPEN, ResponseType.ACCEPT, null);
  79.         if (file_chooser.run() == ResponseType.ACCEPT) create_playbin(file_chooser.get_uri());
  80.         file_chooser.destroy();
  81.     }
  82.  
  83. }
  84.  
  85. public static int main(string[] args)
  86. {
  87.     Gtk.init(ref args);
  88.     Gst.init(ref args);
  89.     var app = new player_window();
  90.     app.show_all();
  91.     Gtk.main();
  92.     return 0;
  93. }
Add Comment
Please, Sign In to add comment