C language Simple video player (ii) based on GTK+LIBVLC implementation

Source: Internet
Author: User
Tags add time gtk


Easy Video Player-full screen playback



I. Description of the course
Last time we used GTK+LIBVLC to achieve a simple video player, can be clicked button Tentative and stop playing video, as well as synchronized display of video playback progress, but even as a video player, only these features are not enough, at least we should also have full-screen playback of the function bar, So this time we're going to add a full screen playback function for the last video player. This function to realize the idea is very simple, but the concrete implementation process has a lot of holes, we need to pay attention to a lot of details, but also to solve some bugs and so on. This time our code has added functionality, and we've made some changes to the previous base code.

Second, the function realizes the thought
Using the API functions provided by GTK, Gtk_window_fullscreen can set the window to full screen, that is, hide the title bar, menu bar, and toolbars while hiding the video control bar at the bottom of the window, preserving only the video playback drawing widget. The playback control and progress display of the video while in full-screen mode is implemented by a separate floating pop-up form. Use the API function Gtk_window_unfullscreen to exit the full-screen state by clicking the Exit Full Screen button on the Float control form, and then restore the control bar widget in normal mode that was previously hidden, and hide the floating pop-up form
650) this.width=650; "src=" Http://anything-about-doc.qiniudn.com/gtk_libvlc_video_player/video.gif "id=" Aimg_ Vumma "class=" Zoom "border=" 0 "height=" 598 "width=" "/>"
Third, to achieve the function point

    • Full screen playback

    • Full-screen status floating control bar

    • Floating control bar and mouse pointer auto-hide when no mouse action

    • Mouse action activates floating control bar and mouse pointer

    • Click Play area pause/play, double-click Full Screen/exit full screen

Four, the function point concrete realization
1. Full Screen playback
Let's start by clicking the Full Screen button to go to full screen, so add a full-screen button to the previous bottom control bar and bind it with a click event handler

Full_screen_button = Gtk_button_new_from_icon_name ("View-fullscreen", Gtk_icon_size_button); Gtk_box_pack_end (GTK_ Box (Hbox), Full_screen_button, False, False, 0); G_signal_connect (Full_screen_button, "clicked", G_callback (On_full_ screen), NULL);



The On_full_screen event handler functions are as follows:

Void on_full_screen (Gtkwidget *widget, gpointer data) {    //  Set normal form into full-screen state     gtk_window_fullscreen (Gtk_window (window));    //  Set a flag that is already in full-screen status, or you can get the window status via GDK, but it will be a little cumbersome     is_fullscreen = TRUE;     //  Hide window bottom control bar and top menu bar     gtk_widget_hide (hbox);     gtk_widget_ Hide (menubar);    //  displays the floating control bar form after the full-screen state, followed by the specific implementation of the form     gtk_ Widget_show_all (Gtk_widget (Ctrl_window));    //  a button icon that syncs to the full-screen state based on the current playback state, Originally consider setting the button icon on the control bar and the     //  on the floating control bar while clicking the button but since there is always a hidden state, the settings will not succeed but will affect the current button icon. It is easy to change the synchronization     if (libvlc_media_player_is_playing (media_player)  == 1) when entering full-screen state alone         play_icon_image = gtk_image_new_from_icon_name (" Media-playback-pause ",  gtk_icon_size_button);      else        play_icon_image = gtk_image_ New_from_icon_name ("Media-playback-start",  gtk_icon_size_button);     gtk_button_set_ Image (Gtk_button (Full_screen_pause_button),  play_icon_image);    ...     //There will also be code that implements the auto-hide floating control bar form, which is explained later


The On_quit_full_screen event handler functions are as follows:

void On_quit_full_screen (Gtkwidget *widget, gpointer data) {Gtk_window_unfullscreen (Gtk_window (window));    Is_fullscreen = FALSE;    Gtk_widget_show (Hbox);    Gtk_widget_show (menubar);      Gtk_widget_hide (Ctrl_window); G_signal_handlers_block_by_func (G_object (Player_widget), on_mouse_motion, NULL);}


2. Create a floating control bar form
Because we are going to create a special form that doesn't have a border, consider this can be created using GTK's pop-up form type gtk_window_popup, and we need to fix the size and position of the form, which is basically the same as the control bar on the bottom of the main form. Here we use a single function to create and initialize the form

Void control_window_init () {    gtkwidget *ctrl_hbox, *quit_full_screen_ Button, *full_screen_stop_button;    ctrl_window = gtk_window_new (GTK_ Window_popup);     gtk_window_set_position (Gtk_window (Ctrl_window),  GTK_WIN_POS_CENTER );     //here two macros are used to set the size of the form     gtk_window_set_default_size (Gtk_window ( Ctrl_window),  ctrl_window_width, ctrl_window_height);     g_signal_connect (Ctrl_ window,  "Destroy",  g_callback (Gtk_main_quit),  null);     ctrl_hbox =  gtk_box_new (gtk_orientation_horizontal, true);     gtk_container_add (GTK_ CONTAINER (Ctrl_window),  ctrl_hbox);     full_screen_pause_button = gtk_ Button_new_from_icon_name ("Media-playback-pause",  gtk_icon_size_button);     full_ Screen_stop_button = gtk_button_new_from_icon_name ("Media-playback-stop",  gtk_icon_size_button);     quit_full_screen _button = gtk_button_new_from_icon_name ("View-restore",  gtk_icon_size_button);     gtk_box_pack_start (Gtk_box (Ctrl_hbox), full_screen_pause_button, false, true,  0);     gtk_box_pack_start (Gtk_box (Ctrl_hbox),  full_screen_stop_button, false,  true, 0);     gtk_box_pack_end (Gtk_box (Ctrl_hbox),  quit_full_screen_button,  false, true, 0);     g_signal_connect (G_object (Full_screen_pause_button),   "Clicked",  g_callback (On_playpause),  null);     g_signal_connect (G_OBJECT ( Full_screen_stop_button),  "clicked",  g_callback (On_stop),  null);     g_ Signal_connect (G_object (Quit_full_screen_button),  "clicked",  g_callback (On_quit_full_screen),  NULL);    &nbSp;ctrl_process_scale = gtk_scale_new (gtk_orientation_horizontal, process_adjuest);     gtk_box_pack_start (Gtk_box (Ctrl_hbox),  ctrl_process_scale, true, true, 0);     gtk_scale_set_draw_value  (Gtk_scale (Ctrl_process_scale),  false);     gtk_scale_set_has_origin  (Gtk_scale (Ctrl_process_scale),  true);     Gtk_scale_set_value_pos (Gtk_scale (Ctrl_process_scale),  0);     g_signal_connect (G_ OBJECT (Ctrl_process_scale), "value_changed",  g_callback (On_scale_value_change),  null);}






3. Set the floating control bar form to hide automatically
Remember the timer that we used to implement the sync progress bar in the last section, G_timeout_add (), which is also implemented by it to auto-hide, we set the timeout 5s to automatically hide the floating form. But what is the time-out to start timing? As we have said before, we start the time when we have no action on the mouse (it should be accurate that the timing is the loop, we just set whether the mouse is in action to hide in the timer)
First we should solve how to know whether the mouse is moving, this will use the Gtk/gdk event, the Motion_notify_event event is triggered when the mouse movement, then we can bind to the Play_widget component event handling of the event, but by default, The Motion_notify_event event of this component is blocked (because different component functions are different, if all events are turned on, it will be very confusing), we have to manually add the event, and we also turn on the mouse click event, the following will be used to achieve the click Play area tentative and so on
Add in the main form initialization function (in the main function of the previous section of the code, this section uses a separate function to create and initialize the main form)

Add time, turn on masking event gtk_widget_add_events (Gtk_widget (player_widget), Gdk_pointer_motion_mask | Gdk_button_press_mask);//bind event handling G_signal_connect (G_object (Player_widget), "Motion_notify_event", G_CALLBACK (On_ mouse_motion), NULL),//In a non-full-screen state first block the event processing, go to full screen and then unblock G_signal_handlers_block_by_func (G_object (player_widget), On_ Mouse_motion, NULL);


We also need to add a timer to the On_full_screen after entering full-screen state


G_signal_handlers_unblock_by_func (G_object (Player_widget), on_mouse_motion, NULL); G_timeout_add (5000, ( Gsourcefunc) _hide_ctrl_window, NULL);

On_mouse_motion event handler function

void On_mouse_motion (Gtkwidget *widget, gpointer data) {//Set whether to move the status flag, avoid the timer timeout during the move to hide the floating form and the mouse arrows is_moving = TRUE;    Mouse has action, recover hidden form gtk_widget_show (Gtk_widget (Ctrl_window));    Restore mouse Arrow gdk_window_set_cursor (Gtk_widget_get_window (gtk_widget (window)), cur);    Reset the floating form display position width = gdk_screen_get_width (Gdk_screen_get_default ());    Height = gdk_screen_get_height (Gdk_screen_get_default ());    Horizontal screen Center, vertical distance from the bottom of the screen 50pix Gtk_window_move (Gtk_window (Ctrl_window), (width-ctrl_window_width)/2, height-50); is_moving = FALSE;}



_hide_ctrl_window Timer handler function

Gboolean _hide_ctrl_window (gpointer data) {if (!is_fullscreen | | is_moving) {return FALSE; }//Set the mouse arrow to Gdk_blank_cursor, which is empty gdk_window_set_cursor (Gtk_widget_get_window (gtk_widget (window)), Gdk_cursor_new (    Gdk_blank_cursor));    Hide floating Control Form gtk_widget_hide (Gtk_widget (Ctrl_window)); return TRUE;}



However, there is a need to pay attention to the place, you may have found that, by default, we do not add auto-hide features, when we move the mouse to the playing area, it will be automatically hidden for a period of time, you may think it is very good, then we can dispense with Gdk_window_set It's _cursor. That's what I thought before, but then I found out there was a problem, and if you just remove the mouse you can hide it, but it won't happen again unless you move out of the display area. Later found that this is the problem of LIBVLC, VLC itself also realized this mouse arrow timeout auto-hide function, you use the following command will find that VLC has a set this timeout time parameters

$ VLC--help | grep Mouse



But I did not find a ban on the function of the parameters, so there is no way, I can only give it a maximum time-out value to achieve the disabled function. In the code we need to manually specify the parameters when creating the Media_player of VLC, as follows:

const char * Const vlc_args[] = {//LIBVLC mouse pointer auto-hide event conflicts with GTK's "motion_notify_event" event cause some problems,//So the maximum time-out is set here (equivalent to banning VLC The function) "--mouse-hide-timeout=2147483647",//hide the cursor after x milliseconds and the full-screen controller "--no-xlib"};...vlc_inst = Libvlc_new (2, Vlc_args); Media_ Player = libvlc_media_player_new (vlc_inst);



4. Click Play area to pause/play, double-click Full Screen/exit full screen
This feature does not need to explain much, basically each player will implement
Earlier we added a click event for Play_widget, just the appropriate processing for its binding event. But how do we decide whether to click or double-tap? This needs to be obtained through gdkevent.
Add Event handling First

G_signal_connect (Player_widget, "Button-press-event", G_callback (on_play_widget_button_press), NULL);



on_play_widget_button_press function implementation

void On_play_widget_button_press (Gtkwidget *widget, gdkevent *event, gpointer data) {//judged to be clicked or double-clicked if (Event->butt    On.type = = gdk_button_press) {///click//directly manually invoke the event handler function previously bound to the pause button on_playpause (widget, data);             } else if (Event->button.type = = gdk_2button_press) {//double-click if (is_fullscreen)//Direct manually call the event handler that was previously bound to exit the full screen button        On_quit_full_screen (widget, data);    else On_full_screen (widget, data); }}



At this point we have achieved all the expected function points.
V. Summary
In this section we added some features to this player, but it's still very simple, but I believe it's enough to be the first to learn about GTK, and then expect the interested user to be able to refine this simple player on their own, making it a really good player for your everyday use ( The following content needs to be run on the virtual platform of the lab building, not add or not, it is to help children's shoes more convenient to learn and understand .

$ git clone-b full_screen https://github.com/shiyanlou/gtk-vlc-video-player.git



Demo Video


wget Http://anything-about-doc.qiniudn.com/gtk_libvlc_video_player/video_demo_02.mp4

There are more basic courses, Project class Welcome to the official website of the experimental building http://www.shiyanlou.com.

Now landing in the experimental building more Thanksgiving good gift both hands http://www.shiyanlou.com/huodong/thanks.html


This article is from the "Lab Building" blog, please be sure to keep this source http://shiyanloucs.blog.51cto.com/9731842/1595083

C language Simple video player (ii) based on GTK+LIBVLC implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.