gtk制作一个音乐盒代码
时间: 2024-01-05 15:19:03 浏览: 84
下面是一个简单的GTK音乐盒的代码示例,包括基本的UI设计和音乐播放逻辑:
```c
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib-object.h>
#include <gst/gst.h>
#include <gst/interfaces/xoverlay.h>
static GtkWidget *window;
static GtkWidget *play_button;
static GtkWidget *stop_button;
static GtkWidget *pause_button;
static GtkWidget *next_button;
static GtkWidget *prev_button;
static GtkWidget *volume_scale;
static GtkWidget *song_list;
static GstElement *playbin;
static GstElement *video_sink;
static GList *songs = NULL;
static GList *current_song = NULL;
/* Forward declarations */
static void load_songs();
static void play_song(GList *song);
static void update_song_list();
/* Callback for the "play" button */
static void on_play_button_clicked(GtkButton *button, gpointer user_data)
{
if (current_song == NULL) {
current_song = g_list_first(songs);
}
play_song(current_song);
}
/* Callback for the "stop" button */
static void on_stop_button_clicked(GtkButton *button, gpointer user_data)
{
gst_element_set_state(playbin, GST_STATE_NULL);
}
/* Callback for the "pause" button */
static void on_pause_button_clicked(GtkButton *button, gpointer user_data)
{
GstState state;
gst_element_get_state(playbin, &state, NULL, GST_CLOCK_TIME_NONE);
if (state == GST_STATE_PLAYING) {
gst_element_set_state(playbin, GST_STATE_PAUSED);
} else if (state == GST_STATE_PAUSED) {
gst_element_set_state(playbin, GST_STATE_PLAYING);
}
}
/* Callback for the "next" button */
static void on_next_button_clicked(GtkButton *button, gpointer user_data)
{
if (current_song == NULL) {
current_song = g_list_first(songs);
} else {
current_song = g_list_next(current_song);
if (current_song == NULL) {
current_song = g_list_first(songs);
}
}
play_song(current_song);
}
/* Callback for the "prev" button */
static void on_prev_button_clicked(GtkButton *button, gpointer user_data)
{
if (current_song == NULL) {
current_song = g_list_last(songs);
} else {
current_song = g_list_previous(current_song);
if (current_song == NULL) {
current_song = g_list_last(songs);
}
}
play_song(current_song);
}
/* Callback for the volume scale */
static void on_volume_scale_changed(GtkRange *range, gpointer user_data)
{
gdouble value = gtk_range_get_value(range);
g_object_set(G_OBJECT(playbin), "volume", value, NULL);
}
/* Load songs from a directory */
static void load_songs()
{
GDir *dir = g_dir_open("music", 0, NULL);
const gchar *filename;
while ((filename = g_dir_read_name(dir)) != NULL) {
gchar *path = g_build_filename("music", filename, NULL);
songs = g_list_append(songs, path);
}
g_dir_close(dir);
}
/* Play a song */
static void play_song(GList *song)
{
gchar *path = song->data;
gst_element_set_state(playbin, GST_STATE_NULL);
g_object_set(G_OBJECT(playbin), "uri", g_filename_to_uri(path, NULL), NULL);
gst_element_set_state(playbin, GST_STATE_PLAYING);
current_song = song;
update_song_list();
}
/* Update the song list */
static void update_song_list()
{
gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(song_list))));
GList *node = songs;
while (node != NULL) {
gchar *path = node->data;
gchar *name = g_path_get_basename(path);
GtkTreeIter iter;
gtk_list_store_append(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(song_list))), &iter);
gtk_list_store_set(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(song_list))), &iter, 0, name, -1);
if (node == current_song) {
gtk_list_store_set(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(song_list))), &iter, 1, TRUE, -1);
}
g_free(name);
node = g_list_next(node);
}
}
int main(int argc, char *argv[])
{
/* Initialize GTK+ */
gtk_init(&argc, &argv);
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Create the main window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GTK Music Box");
gtk_window_set_default_size(GTK_WINDOW(window), 500, 300);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
/* Create the main layout */
GtkBox *layout = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(layout));
/* Create the song list */
GtkScrolledWindow *song_list_scroll = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
gtk_scrolled_window_set_policy(song_list_scroll, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start(layout, GTK_WIDGET(song_list_scroll), TRUE, TRUE, 0);
song_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_BOOLEAN)));
GtkTreeViewColumn *column = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(column, "Song");
gtk_tree_view_append_column(GTK_TREE_VIEW(song_list), column);
GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(column, renderer, TRUE);
gtk_tree_view_column_add_attribute(column, renderer, "text", 0);
renderer = gtk_cell_renderer_toggle_new();
gtk_tree_view_column_pack_start(column, renderer, FALSE);
gtk_tree_view_column_add_attribute(column, renderer, "active", 1);
gtk_container_add(GTK_CONTAINER(song_list_scroll), GTK_WIDGET(song_list));
/* Load the songs */
load_songs();
/* Create the play button */
play_button = gtk_button_new_with_label("Play");
gtk_box_pack_start(layout, play_button, FALSE, FALSE, 0);
g_signal_connect(play_button, "clicked", G_CALLBACK(on_play_button_clicked), NULL);
/* Create the stop button */
stop_button = gtk_button_new_with_label("Stop");
gtk_box_pack_start(layout, stop_button, FALSE, FALSE, 0);
g_signal_connect(stop_button, "clicked", G_CALLBACK(on_stop_button_clicked), NULL);
/* Create the pause button */
pause_button = gtk_button_new_with_label("Pause");
gtk_box_pack_start(layout, pause_button, FALSE, FALSE, 0);
g_signal_connect(pause_button, "clicked", G_CALLBACK(on_pause_button_clicked), NULL);
/* Create the next button */
next_button = gtk_button_new_with_label("Next");
gtk_box_pack_start(layout, next_button, FALSE, FALSE, 0);
g_signal_connect(next_button, "clicked", G_CALLBACK(on_next_button_clicked), NULL);
/* Create the prev button */
prev_button = gtk_button_new_with_label("Prev");
gtk_box_pack_start(layout, prev_button, FALSE, FALSE, 0);
g_signal_connect(prev_button, "clicked", G_CALLBACK(on_prev_button_clicked), NULL);
/* Create the volume scale */
volume_scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0.0, 1.0, 0.1);
gtk_box_pack_start(layout, volume_scale, FALSE, FALSE, 0);
g_signal_connect(volume_scale, "value-changed", G_CALLBACK(on_volume_scale_changed), NULL);
/* Create the audio player */
playbin = gst_element_factory_make("playbin", NULL);
g_object_set(G_OBJECT(playbin), "video-sink", NULL, NULL);
g_object_set(G_OBJECT(playbin), "volume", gtk_range_get_value(GTK_RANGE(volume_scale)), NULL);
/* Start the main loop */
update_song_list();
gtk_widget_show_all(window);
gtk_main();
return 0;
}
```
这是一个简单的GTK音乐盒的代码,你可以根据你的需求进行修改。
阅读全文