Martin A has asked for the wisdom of the Perl Monks concerning the following question:

Hi there.

I am currently working on a mp3 player using gtk2-perl for the GUI and Audio::Play::MPG123 module for mp3 playback. I know that this probably have been done a trillion times before, but this one fits my special needs.

Now I have run in to a problem which I can't really decide how to tackle:

In my GUI i have a progressbar/timeline (GtkHScale widget) which needs to be continuously updated with the position in the current playing song.

When you start your Gtk GUI you run the Gtk2->main() function.That function controls the flow of the program and only gives me a chance to run code when a GUI event occur (eg. button pressed runs some sub etc.)

This leads me to the question:
How would I go about having the timeline continuously updated when I don't control the flow of the program?

I have some ideas:
(These are just ideas, I don't know if they would work or not)
treads: have some kind of function that updates the timeline running in a separate thread.
timerObject: maybe there is some glib/gtk/perl object that can run a given function several times in a given interval.


Since I'm kind of stuck I appreciate any help I can get.

Martin A -- Just Another Perl Hooker

Replies are listed 'Best First'.
Re: GUI (Gtk2) Programming problem
by Aristotle (Chancellor) on Dec 27, 2003 at 23:53 UTC
    You want a Glib::Timeout. Installing one causes the main loop to periodically run your callback in the defined intervalls. There are also similar hooks available for callbacks run when the program is idle or when a file handle has activity.

    Makeshifts last the longest.

Re: GUI (Gtk2) Programming problem
by hossman (Prior) on Dec 27, 2003 at 23:23 UTC

    When I first saw your post this morning, i was going to reply that you should register an "Alarm", and then in your alarm method poll mpg123 for the current frame, and update your progress bar accordingly, but i didn't see any mention of "Alarm" in the Gtk docs (at least not the sense i ment) so i walked away from my computer for a while, did some stuff, and when i came back and saw there was still no replies, i did some digging.

    The concept i was thinking of is called a "Timeout". There is a pretty good example of using timeouts with progress bar in this tutorial

Re: GUI (Gtk2) Programming problem
by revdiablo (Prior) on Dec 27, 2003 at 23:32 UTC

    I haven't done any plain Perl Gtk programming, but I've used Gtk with POE. I'm not sure how applicable it is to plain Gtk, but here goes.

    I wrote a CD ripping program, and had a similar problem. I wanted to update the progress bar each time a track was finished, but my loop was hogging the whole program. Since POE doesn't use threads, any event that runs has the whole program. What I ended up doing was breaking the loop up into several separate events. Each time a track finishes, it does two things: update the progress bar, then trigger the event that rips the next track.

    I don't know if Gtk's main loop exposes the events to you so explicitly, and I don't know if you'll be able to realistically trigger an update each time a frame of the mp3 plays, or whatever. Perhaps adding a UI thread and a player thread is the best idea, but it's just some food for thought.