1 // gcc -o gtk_player gtk_player.c `pkg-config --libs gtk+-2.0 libvlc` `pkg-config --cflags gtk+-2.0 libvlc`
3 /* License WTFPL http://sam.zoy.org/wtfpl/ */
4 /* Written by Vincent Schüßler */
11 #define BORDER_WIDTH 6
13 void destroy(GtkWidget
*widget
, gpointer data
);
14 void player_widget_on_realize(GtkWidget
*widget
, gpointer data
);
15 void on_open(GtkWidget
*widget
, gpointer data
);
16 void open_media(const char* uri
);
18 void pause_player(void);
19 void on_playpause(GtkWidget
*widget
, gpointer data
);
20 void on_stop(GtkWidget
*widget
, gpointer data
);
22 libvlc_media_player_t
*media_player
;
23 libvlc_instance_t
*vlc_inst
;
24 GtkWidget
*playpause_button
;
26 void destroy(GtkWidget
*widget
, gpointer data
) {
30 void player_widget_on_realize(GtkWidget
*widget
, gpointer data
) {
31 libvlc_media_player_set_xwindow(media_player
, GDK_WINDOW_XID(gtk_widget_get_window(widget
)));
34 void on_open(GtkWidget
*widget
, gpointer data
) {
36 dialog
= gtk_file_chooser_dialog_new("Choose Media", data
, GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_STOCK_CANCEL
,GTK_RESPONSE_CANCEL
, GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
37 if(gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
39 uri
= gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog
));
43 gtk_widget_destroy(dialog
);
46 void open_media(const char* uri
) {
47 libvlc_media_t
*media
;
48 media
= libvlc_media_new_location(vlc_inst
, uri
);
49 libvlc_media_player_set_media(media_player
, media
);
51 libvlc_media_release(media
);
54 void on_playpause(GtkWidget
*widget
, gpointer data
) {
55 if(libvlc_media_player_is_playing(media_player
) == 1) {
63 void on_stop(GtkWidget
*widget
, gpointer data
) {
65 libvlc_media_player_stop(media_player
);
69 libvlc_media_player_play(media_player
);
70 gtk_button_set_label(GTK_BUTTON(playpause_button
), "gtk-media-pause");
73 void pause_player(void) {
74 libvlc_media_player_pause(media_player
);
75 gtk_button_set_label(GTK_BUTTON(playpause_button
), "gtk-media-play");
78 int main( int argc
, char *argv
[] ) {
89 gtk_init (&argc
, &argv
);
91 window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
92 gtk_window_set_default_size(GTK_WINDOW(window
), 400, 300);
93 g_signal_connect(window
, "destroy", G_CALLBACK(destroy
), NULL
);
94 gtk_container_set_border_width (GTK_CONTAINER (window
), 0);
95 gtk_window_set_title(GTK_WINDOW(window
), "GTK+ libVLC Demo");
98 vbox
= gtk_vbox_new(FALSE
, 0);
99 gtk_container_add(GTK_CONTAINER(window
), vbox
);
102 menubar
= gtk_menu_bar_new();
103 filemenu
= gtk_menu_new();
104 fileitem
= gtk_menu_item_new_with_label ("File");
105 filemenu_openitem
= gtk_menu_item_new_with_label("Open");
106 gtk_menu_shell_append(GTK_MENU_SHELL(filemenu
), filemenu_openitem
);
107 gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileitem
), filemenu
);
108 gtk_menu_bar_append(GTK_MENU_BAR(menubar
), fileitem
);
109 gtk_box_pack_start(GTK_BOX(vbox
), menubar
, FALSE
, FALSE
, 0);
110 g_signal_connect(filemenu_openitem
, "activate", G_CALLBACK(on_open
), window
);
112 //setup player widget
113 player_widget
= gtk_drawing_area_new();
114 gtk_box_pack_start(GTK_BOX(vbox
), player_widget
, TRUE
, TRUE
, 0);
117 //playpause_button = gtk_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
118 playpause_button
= gtk_button_new_with_label("gtk-media-play");
119 gtk_button_set_use_stock(GTK_BUTTON(playpause_button
), TRUE
);
120 stop_button
= gtk_button_new_from_stock(GTK_STOCK_MEDIA_STOP
);
121 g_signal_connect(playpause_button
, "clicked", G_CALLBACK(on_playpause
), NULL
);
122 g_signal_connect(stop_button
, "clicked", G_CALLBACK(on_stop
), NULL
);
123 hbuttonbox
= gtk_hbutton_box_new();
124 gtk_container_set_border_width(GTK_CONTAINER(hbuttonbox
), BORDER_WIDTH
);
125 gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox
), GTK_BUTTONBOX_START
);
126 gtk_box_pack_start(GTK_BOX(hbuttonbox
), playpause_button
, FALSE
, FALSE
, 0);
127 gtk_box_pack_start(GTK_BOX(hbuttonbox
), stop_button
, FALSE
, FALSE
, 0);
128 gtk_box_pack_start(GTK_BOX(vbox
), hbuttonbox
, FALSE
, FALSE
, 0);
131 vlc_inst
= libvlc_new(0, NULL
);
132 media_player
= libvlc_media_player_new(vlc_inst
);
133 g_signal_connect(G_OBJECT(player_widget
), "realize", G_CALLBACK(player_widget_on_realize
), NULL
);
135 gtk_widget_show_all(window
);
138 libvlc_media_player_release(media_player
);
139 libvlc_release(vlc_inst
);