2009-01-09

./jamendo-player

I was enough bored from my (small) music collection that I wanted to listen to something new, without actually caring what it is. I know Jamendo has a big load of music with artists uploading music under creative commons licence. So I went on their website to have a look, and they have "widgets". They are a small flash application not bigger than 200x300, but I actually didn't find any link to open such a widget inside an external window, except some lost links on some pages. In conclusion it was enough annoying to get an external player that I fired up vim and wrote a 20 lines C program.

/* gcc `pkg-config --cflags --libs webkit-1.0` main.c -o jamendo-player */

#include <gtk/gtk.h>
#include <webkit/webkit.h>

#define URL_FORMAT "http://widgets.jamendo.com/fr/playlist/?playertype=2008&playlist_id=%s"

int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *webview;
gchar *url;
gchar *playlist_id = argv[1];
if (argc < 2)
{
playlist_id = g_strdup ("65196");
g_message ("Loading the default playlist %s", playlist_id);
}
url = g_strdup_printf (URL_FORMAT, playlist_id);
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_icon_name (GTK_WINDOW (window), "multimedia-player");
gtk_window_set_title (GTK_WINDOW (window), "Jamendo Player");
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
webview = webkit_web_view_new ();
gtk_widget_set_size_request (webview, 200, 300);
gtk_container_add (GTK_CONTAINER (window), webview);
webkit_web_view_open (WEBKIT_WEB_VIEW (webview), url);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}


The result is pleasing, the flash application has nice colors, and is something I would never be able to do with GTK+ :-)

If you like the code, feel free to extend it and send patches, I will enjoy any contribution. For now I will just leave a desktop file inside my applications directory to open it when I am bored again of my music :-p

Update: I hacked a little on the program to handle the links inside the Flash application. This means it is possible to change the playlist, which was one the thing that started to annoy me very much.

However, this update requires WebKit 1.0.3 and GLib 2.16.

video

This time I'm making the code available here instead of pasting it as it has grown up to 120 lines.

19 comments:

  1. Can't compile it. I don't know anything about compiling, so I made:

    $ gcc `pkg-config --cflags --libs webkit-1.0` main2.c -o jamendo-player

    And it says:

    main2.c:3:9: error: #include expects "FILENAME" or FILENAME
    main2.c:4:9: error: #include expects "FILENAME" or FILENAME
    main2.c: In function 'main':
    main2.c:10: error: 'GtkWidget' undeclared (first use in this function)
    main2.c:10: error: (Each undeclared identifier is reported only once
    main2.c:10: error: for each function it appears in.)
    main2.c:10: error: 'window' undeclared (first use in this function)
    main2.c:11: error: 'webview' undeclared (first use in this function)
    main2.c:12: error: 'gchar' undeclared (first use in this function)
    main2.c:12: error: 'url' undeclared (first use in this function)
    main2.c:13: error: 'playlist_id' undeclared (first use in this function)
    main2.c:21: error: 'GTK_WINDOW_TOPLEVEL' undeclared (first use in this function)
    main2.c:22: error: 'gtk_main_quit' undeclared (first use in this function)
    main2.c:22: error: 'NULL' undeclared (first use in this function)
    main2.c:25: error: 'FALSE' undeclared (first use in this function)

    I edited the ouput in FILENAME to pass the coments filter.

    Prior I installed (in Debian/Lenny) libwebkit-dev and his dependencies.

    Thanks. in advance. Martintxo.

    ReplyDelete
  2. Heh, you should try again. When I made the post I didn't realize there was a problem with the #includes, in fact the #include <gtk/gtk.h> and <webkit/webkit.h> were read as HTML tags.

    ReplyDelete
  3. compile okay but when run, it says:

    GLib-ERROR **: The thread system is not yet initialized.

    ReplyDelete
  4. The same stuff using Vala:

    // valac --pkg gtk+-2.0 --pkg webkit-1.0 -o jamendo-player jamendo.vala

    using Gtk;
    using WebKit;

    public class JamendoPlayer : Gtk.Window {

    private WebView webWidget;

    public JamendoPlayer(string playlist) {
    this.destroy += Gtk.main_quit;

    this.icon_name = "multimedia-player";
    this.title = "Jamendo Player";
    this.resizable = false;
    set_size_request (200, 300);

    webWidget = new WebView();

    webWidget.open("http://widgets.jamendo.com/de/playlist/?playertype=2008&playlist_id="+playlist);
    add(webWidget);
    }


    public static int main (string[] args) {
    string playlist = "65196";
    JamendoPlayer player;

    Gtk.init(ref args);

    if (args.length == 2)
    playlist = args[1];

    player = new JamendoPlayer(playlist);
    player.show_all();

    Gtk.main();

    return 0;
    }
    }

    ReplyDelete
  5. @lawcp: Weird. Maybe this indicates that you have a different version of gtk+/glib. Try adding g_thread_init(NULL); before the gtk_init() line.

    Nice shot with the vala code :)

    ReplyDelete
  6. I was just told that the error with the thread is caused by webkit build with libsoup, and that it is going to be mandatory to init the thread system.

    ReplyDelete
  7. Hm... Nice work, but:

    (jamendo-player:31703): GLib-GObject-WARNING **: /build/buildd/glib2.0-2.16.6/gobject/gsignal.c:1669: signal `create-web-view' is invalid for instance `0x66f000'

    ReplyDelete
  8. I bet you do not have webkit 1.0.3 or higher

    ReplyDelete
  9. Yes, u're right. My fault, sorry.

    ReplyDelete
  10. I've compiled it successfuly, but it shows nothing on start. what is the problem? do you have any idea?

    ReplyDelete
  11. That's because Jamendo always changes its players. You might wish to try the Shoutcast player.

    Otherwise you can always search on Jamendo for a player.

    ReplyDelete
  12. Rrright, the shoutcast-radio also shows a blank page. But given their website, it looks like they totally dropped the Flash player.

    ReplyDelete
  13. I've checked URL_FORMAT against the jamendo latest widget script and they are still exactly same. I've checked your shoutcast player, too but same result unfortunately :( They doesn't show :$ Strange!

    ReplyDelete
  14. By the way, i'm aware of other player posibilities, but i'm another C programmer like you and that's why, i'm interested in your code. I want to see it's working (:. Thanks anyway, good to discover your blog. (:

    ReplyDelete
  15. verdim şukunu süper olmuş ;)

    ReplyDelete
  16. Not sure why this is happening, I will have to ask a WebKit colleague (although I don't really care to know). Loading a Flash "page" does not work, it has to be done through an intermediate HTML page.

    http://pastebin.com/YXCQKtUY

    ReplyDelete
  17. Btw, I really don't know what "şukunu" means!

    ReplyDelete
  18. Thank you Mike, It's working now. I liked webkit and i'll work on it a bit more. Actually, i dont care why this is happening indeed. (:

    And about "şukunu", he is a friend of mine. I asked him and he said that it's an idiom and it means something like +1 rep. He tried to say "i liked it". Not so important. He is a little bit crazy. (:

    ReplyDelete