libvlc和gtk(用于GUI)不能一起工作?

时间:2012-08-03 02:41:30

标签: gtk libvlc

我正在尝试使用libvlc和gtk +而没有成功。我认为我用于GUI的libvlc_media_player_set_xwindow()函数存在问题。我的代码如下:

#include <stdio.h>  
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include <vlc/vlc.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>

libvlc_media_t *media_played;
libvlc_media_player_t *is_play;
void Play(gpointer frame, GtkButton * button);
int main(int argc, char **argv)
{
  libvlc_instance_t * instance = libvlc_new(0,NULL);
  libvlc_media_player_t *player = libvlc_media_player_new(instance);
  is_play = player;
  gtk_init(&argc, &argv);
  char Files[256];
  int i =0;
  printf("enter file name plus path, replace scape by '=' character\n");
  scanf("%s", Files);
  for(i = 0; i < 256; i++)
  {
      if(Files[i] == '=') Files[i] = ' ';
  }
  libvlc_media_t *media = libvlc_media_new_path(instance, Files);
  media_played = media;
  libvlc_media_player_set_media(player, media);
  GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  GtkWidget * table = gtk_table_new(1,2,FALSE);
  GtkWidget * table1 = gtk_table_new(1,1,TRUE);
  GtkWidget *frame = gtk_frame_new(NULL);
  gtk_frame_set_shadow_type(GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
  GtkWidget *button = gtk_button_new_with_mnemonic("_Play");
  g_signal_connect_swapped(GTK_BUTTON(button),"clicked",G_CALLBACK(Play),(gpointer)frame);  
  libvlc_media_player_set_xwindow(player, GDK_WINDOW_XID(frame->window));
  gtk_table_attach(GTK_TABLE(table1),button,0,1,0,1,GTK_FILL,GTK_FILL,0,0);
  gtk_table_attach(GTK_TABLE(table),table1,0,1,0,1,GTK_FILL,GTK_FILL,0,0);
  gtk_table_attach(GTK_TABLE(table),frame,1,2,0,1,GTK_FILL,GTK_FILL,0,0);
  gtk_container_add(GTK_CONTAINER(window),table);
  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}

void Play(gpointer frame, GtkButton * button)
{
   char * title = libvlc_media_get_meta(media_played, libvlc_meta_Title);
   printf("vlc will play: %s\n", title);
   int64_t Time = libvlc_media_get_duration(media_played);
   libvlc_media_player_play(is_play);
   Time = Time/1000;
   clock_t c = clock();
   clock_t t = c;
   while((c/CLOCKS_PER_SEC)<( Time + t))
   {
        c = c + clock();
   }
   gtk_widget_destroy(((GTK_WIDGET(frame)->parent)->parent));
   gtk_main_quit();
}  

运行时,它会给我一个错误:

(vlc1:7774): Gdk-WARNING **: /build/buildd/gtk+2.0-2.24.10/gdk/x11/gdkdrawable-x11.c:952 drawable is not a pixmap or window 

有人有解决方案吗?

1 个答案:

答案 0 :(得分:1)

问题很可能是因为当您在代码中拨打电话libvlc_media_player_set_xwindow时,frame(或更正确的顶级窗口)尚未实现且与GdkWindow相关联,即尚未创建frame->window。在代码中libvlc_media_player_set_xwindow之后移动电话gtk_widget_show_all(window);并检查您的申请 最好使用gtk_widget_get_window代替解除引用GtkWidget指针来获取关联的GdkWindow,即gtk_widget_get_window(frame)而不是frame->window
希望这有帮助!