我正在谷歌搜索,以便在我的c ++程序(媒体播放器)中使用gstreamer库。我查看了这些链接: 1,2,3。此时我可以打开媒体文件并在绘图区域内播放。无论如何,当我再次想要播放另一个媒体时,它会在另一个新窗口中打开。
我为此疯狂,因为参考文献非常不完整,我在谷歌上发现了一些东西。
这是我的“playMedia”功能:
void MediaPlayer::playMedia(const string &filename) {
gst_element_set_state(playerData.playbin2, GST_STATE_READY);
gchar *uri = g_filename_to_uri(filename.c_str(),NULL,NULL);
if (uri) {
g_object_set(playerData.playbin2, "uri", uri, NULL);
g_object_set(G_OBJECT(playerData.videosink), "force-aspect-ratio", TRUE, NULL);
g_free(uri);
gtk_widget_realize(areaPlayer); ì
play();
}
}
这就是“播放”功能:
void MediaPlayer::play() {
state = PLAYING;
gtk_button_set_image(GTK_BUTTON(buttonPlayPause), imgPause);
g_object_ref(imgPause);
gst_element_set_state(playerData.playbin2, GST_STATE_PLAYING);
}
附加
以下是我定义areaPlayer(gtk_drawing_area)的代码部分:
gst_init(0, 0);
playerData.playbin2 = gst_element_factory_make("playbin2", NULL);
playerData.videosink = gst_element_factory_make ("xvimagesink", NULL);
g_object_set(G_OBJECT(playerData.playbin2), "video-sink", playerData.videosink, NULL);
playerData.duration = GST_CLOCK_TIME_NONE;
areaPlayer = gtk_drawing_area_new();
gtk_widget_set_double_buffered (areaPlayer, FALSE);
gtk_box_pack_start(GTK_BOX(theWidget), areaPlayer, true, true, 0);
g_signal_connect(areaPlayer, "realize", G_CALLBACK(this->playerRealize), this);
g_signal_connect(areaPlayer, "draw", G_CALLBACK(this->playerDraw), this);
这是实现功能
void MediaPlayer::playerRealize(GtkWidget *widget, gpointer data) {
MediaPlayer *m = (MediaPlayer *)data;
GdkWindow *window = gtk_widget_get_window(widget);
guintptr window_handle;
if (!gdk_window_ensure_native(window))
cout << "Couldn't create native window needed for GstXOverlay!" << endl;
// Retrieve window handler from GDK
window_handle = GDK_WINDOW_XID(window);
// Pass it to playbin2, which implements XOverlay and will forward it to the video sink
gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (m->playerData.videosink), window_handle);
}