LibVLC Tutorial 0.9

From VideoLAN Wiki
Revision as of 01:14, 30 March 2008 by Pdherbemont (talk | contribs) (→‎LibVLC Tutorial: convert to media and media_player)
Jump to navigation Jump to search

LibVLC Tutorial

This may work with trunk version, does not work with 0.8.6c

Linking against LibVLC

cc example.c -lvlc-control -o example

Sample LibVLC Code

#include <stdio.h>
#include <vlc/libvlc.h>

static void quit_on_exception( libvlc_exception_t * excp )
{
    if( libvlc_exception_raised( &excp );
    {
         fprintf(stderr, "error: %s\n", libvlc_exception_get_message(excp) );
         exit(-1);
    }
}
int main(int argc, char* argv[])
{
    const char * const vlc_args[] = {"-I", "dummy", "--module-path=/set/your/path/to/libvlc/module/if/you/are/on/windows/or/macosx" };
    libvlc_exception_t excp;
    libvlc_instance_t * inst;
    libvlc_media_player_t *mp;
    libvlc_media_t *m;
    
    libvlc_exception_init( &excp );
    /* init vlc modules, should be done only once */
    inst = libvlc_new( sizeof(args) / sizeof(args[0]), arg, &excp );
    quit_on_exception( &excp );
 
    /* Create a new item */
    m = libvlc_media_new( inst, "http://mycool.movie.com/test.mov", &excp );
    quit_on_exception( &excp );
   
    /* XXX: demo art and meta information fetching */
   
    /* Create a media player playing environement */
    mp = libvlc_media_player_new_from_media_descriptor( m, &excp );
    quit_on_exception( &excp );
    
    /* No need to keep the media now */
    libvlc_media_release( m );

#if 0
    /* This is a non working code that show how to hooks into a window,
     * if we have a window around */
     libvlc_drawable_t drawable = xdrawable;
    /* or on windows */
     libvlc_drawable_t drawable = hwnd;

     libvlc_media_player_set_drawable( mp, drawable, &excp );
     quit_on_exception( &excp );
#endif

    /* play the media_player */
    libvlc_media_player_play( mp, &excp );
    quit_on_exception( &excp );
   
    sleep(10); /* Let it play a bit */
   
    /* Stop playing */
    libvlc_media_player_stop( mp, &excp );

    /* Free the media_player */
    libvlc_media_player_release( mp, &excp );

    libvlc_release( inst, &excp );
    quit_on_exception( &excp );

    return 0;
}