ocarina: Update slider as track plays
Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
70ef389aab
commit
d6d455ab7f
|
@ -7,6 +7,9 @@
|
|||
|
||||
static GtkWidget *notebook;
|
||||
static GtkWidget *idle_progress;
|
||||
static GtkWidget *pos_label;
|
||||
static GtkWidget *track_progress;
|
||||
static GtkWidget *dur_label;
|
||||
static GtkWidget *play_button;
|
||||
static GtkWidget *pause_button;
|
||||
static GstState last_state;
|
||||
|
@ -21,6 +24,25 @@ static void add_page(string text, GtkWidget *page)
|
|||
gtk_widget_show(label);
|
||||
}
|
||||
|
||||
static void update_progress()
|
||||
{
|
||||
gint64 pos = libsaria::audio::position();
|
||||
gint64 dur = libsaria::audio::duration() + 1;
|
||||
|
||||
gtk_label_set_text(GTK_LABEL(pos_label), libsaria::audio::posstr().c_str());
|
||||
gtk_label_set_text(GTK_LABEL(dur_label), libsaria::audio::durstr().c_str());
|
||||
|
||||
/*
|
||||
* This happens when gstreamer's "about-to-finish" signal
|
||||
* is emitted and we queue up a shorter song before the
|
||||
* pipeline finishes.
|
||||
*/
|
||||
if (pos > dur)
|
||||
pos = 0;
|
||||
gtk_range_set_range(GTK_RANGE(track_progress), 0, dur);
|
||||
gtk_range_set_value(GTK_RANGE(track_progress), pos);
|
||||
}
|
||||
|
||||
static void on_click_play(GtkWidget *b, GdkEvent *e, gpointer d)
|
||||
{
|
||||
libsaria::audio::play();
|
||||
|
@ -34,14 +56,26 @@ static void on_click_pause(GtkWidget *b, GdkEvent *e, gpointer d)
|
|||
static GtkWidget *make_controls()
|
||||
{
|
||||
GtkWidget *box = gtk_hbox_new(FALSE, 0);
|
||||
|
||||
play_button = make_button(GTK_STOCK_MEDIA_PLAY, on_click_play, true);
|
||||
pause_button = make_button(GTK_STOCK_MEDIA_PAUSE, on_click_pause, false);
|
||||
|
||||
pos_label = gtk_label_new("");
|
||||
track_progress = gtk_hscale_new_with_range(0, 1, 1000000000);
|
||||
dur_label = gtk_label_new("");
|
||||
gtk_scale_set_draw_value(GTK_SCALE(track_progress), FALSE);
|
||||
|
||||
idle_progress = gtk_progress_bar_new();
|
||||
gtk_box_pack_start(GTK_BOX(box), idle_progress, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(box), pos_label, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(box), track_progress, TRUE, TRUE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(box), dur_label, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(box), play_button, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(box), pause_button, FALSE, FALSE, 0);
|
||||
|
||||
gtk_widget_show(pos_label);
|
||||
gtk_widget_show(dur_label);
|
||||
gtk_widget_show(track_progress);
|
||||
gtk_widget_show(box);
|
||||
return box;
|
||||
}
|
||||
|
@ -83,9 +117,14 @@ namespace ocarina
|
|||
|
||||
void body::update_controls()
|
||||
{
|
||||
GstState cur_state = libsaria::audio::get_state();
|
||||
GstState cur_state;
|
||||
|
||||
update_progress();
|
||||
|
||||
cur_state = libsaria::audio::get_state();
|
||||
if (cur_state == last_state)
|
||||
return;
|
||||
|
||||
last_state = cur_state;
|
||||
if (cur_state == GST_STATE_PLAYING) {
|
||||
gtk_widget_show(pause_button);
|
||||
|
|
Loading…
Reference in New Issue