Difference between revisions of "LibVLC Tutorial"

From VideoLAN Wiki
Jump to navigation Jump to search
m (syntaxhighlight)
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
== LibVLC Tutorial ==
+
{{Lowercase}}
 +
{{Example code}}
 +
== libVLC C Tutorial ==
  
This code is written for LibVLC version 1.1.0 or later. It '''cannot''' be compiled with older versions.
+
=== Installing the SDK ===
 +
This code is written for [[libVLC]] version 1.1.0 or later. It '''cannot''' be compiled with older versions.
  
Note that VLC binary installers for Windows do not include the LibVLC SDK (it would be a waste of bandwidth and space for most users). See [[LibVLC_Tutorial_086c]] for some instructions on getting the build working.
+
Older versions are found at [[libVLC Tutorial 0.9]] and [[libVLC Tutorial 086c]] respectively for historical interest only.
  
=== Linking against LibVLC ===
+
==== Windows ====
 +
VLC binary installers for Windows do not include the libVLC {{SDK}}. It would be a waste of bandwidth and space for most users.
 +
 
 +
You can find the SDK files, i.e. development headers and import libraries, from the plain 7-ZIP packages in the <code>sdk</code> directory. Alternatively, you can build VLC from source.
 +
 
 +
See [[libVLC Tutorial 086c]] for some more instructions on getting the build working.
 +
 
 +
==== Linux ====
 +
You should find the necessary files (libvlc.so, libvlc.pc, header files...) in a binary package called libvlc-dev (Debian, Ubuntu...), libvlc-devel (RPM distros) or similar. Make sure that the version is recent enough before going forward.
 +
 
 +
=== Linking against libVLC ===
  
 
<pre>
 
<pre>
Line 11: Line 24:
 
</pre>
 
</pre>
  
=== Sample LibVLC Code ===
+
On Linux/BSD, you may prefer to use pkg-config:
 +
<syntaxhighlight lang="bash">
 +
pkg-config --print-errors 'libvlc >= 1.1.0'
 +
cc -c example.c -o example.o $(pkg-config --cflags libvlc)
 +
cc example.o -o example $(pkg-config --libs libvlc)
 +
</syntaxhighlight>
  
<pre>
+
=== Sample libVLC code ===
 +
This sample code will (try to) play back an URL.
 +
There is also an [[libVLC SampleCode SDL|example using SDL for video output]].
 +
<syntaxhighlight lang="c">
 
  #include <stdio.h>
 
  #include <stdio.h>
 
  #include <stdlib.h>
 
  #include <stdlib.h>
Line 20: Line 41:
 
  int main(int argc, char* argv[])
 
  int main(int argc, char* argv[])
 
  {
 
  {
    const char * const vlc_args[] = {
 
              "-I", "dummy", /* Don't use any interface */
 
              "--ignore-config", /* Don't use VLC's config */
 
              "--plugin-path=/set/your/path/to/libvlc/module/if/you/are/on/windows/or/macosx" };
 
 
     libvlc_instance_t * inst;
 
     libvlc_instance_t * inst;
 
     libvlc_media_player_t *mp;
 
     libvlc_media_player_t *mp;
 
     libvlc_media_t *m;
 
     libvlc_media_t *m;
 
      
 
      
     /* init vlc modules, should be done only once */
+
     /* Load the VLC engine */
     inst = libvlc_new (sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);
+
     inst = libvlc_new (0, NULL);
 
    
 
    
 
     /* Create a new item */
 
     /* Create a new item */
     m = libvlc_media_new (inst, "http://mycool.movie.com/test.mov");
+
     m = libvlc_media_new_location (inst, "http://mycool.movie.com/test.mov");
 +
    //m = libvlc_media_new_path (inst, "/path/to/test.mov");
 
          
 
          
 
     /* Create a media player playing environement */
 
     /* Create a media player playing environement */
Line 43: Line 61:
 
     /* This is a non working code that show how to hooks into a window,
 
     /* This is a non working code that show how to hooks into a window,
 
       * if we have a window around */
 
       * if we have a window around */
       libvlc_media_player_set_xdrawable (mp, xdrawable);
+
       libvlc_media_player_set_xwindow (mp, xid);
 
     /* or on windows */
 
     /* or on windows */
 
       libvlc_media_player_set_hwnd (mp, hwnd);
 
       libvlc_media_player_set_hwnd (mp, hwnd);
Line 65: Line 83:
 
     return 0;
 
     return 0;
 
  }
 
  }
</pre>
+
</syntaxhighlight>
 +
 
 +
[[Category:libVLC]]

Latest revision as of 08:57, 4 March 2019

This page contains example code.

libVLC C Tutorial

Installing the SDK

This code is written for libVLC version 1.1.0 or later. It cannot be compiled with older versions.

Older versions are found at libVLC Tutorial 0.9 and libVLC Tutorial 086c respectively for historical interest only.

Windows

VLC binary installers for Windows do not include the libVLC SDK. It would be a waste of bandwidth and space for most users.

You can find the SDK files, i.e. development headers and import libraries, from the plain 7-ZIP packages in the sdk directory. Alternatively, you can build VLC from source.

See libVLC Tutorial 086c for some more instructions on getting the build working.

Linux

You should find the necessary files (libvlc.so, libvlc.pc, header files...) in a binary package called libvlc-dev (Debian, Ubuntu...), libvlc-devel (RPM distros) or similar. Make sure that the version is recent enough before going forward.

Linking against libVLC

cc example.c -lvlc -o example

On Linux/BSD, you may prefer to use pkg-config:

pkg-config --print-errors 'libvlc >= 1.1.0'
cc -c example.c -o example.o $(pkg-config --cflags libvlc)
cc example.o -o example $(pkg-config --libs libvlc)

Sample libVLC code

This sample code will (try to) play back an URL. There is also an example using SDL for video output.

 #include <stdio.h>
 #include <stdlib.h>
 #include <vlc/vlc.h>
 
 int main(int argc, char* argv[])
 {
     libvlc_instance_t * inst;
     libvlc_media_player_t *mp;
     libvlc_media_t *m;
     
     /* Load the VLC engine */
     inst = libvlc_new (0, NULL);
  
     /* Create a new item */
     m = libvlc_media_new_location (inst, "http://mycool.movie.com/test.mov");
     //m = libvlc_media_new_path (inst, "/path/to/test.mov");
        
     /* Create a media player playing environement */
     mp = libvlc_media_player_new_from_media (m);
     
     /* 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_media_player_set_xwindow (mp, xid);
     /* or on windows */
      libvlc_media_player_set_hwnd (mp, hwnd);
     /* or on mac os */
      libvlc_media_player_set_nsobject (mp, view);
  #endif
 
     /* play the media_player */
     libvlc_media_player_play (mp);
    
     sleep (10); /* Let it play a bit */
    
     /* Stop playing */
     libvlc_media_player_stop (mp);
 
     /* Free the media_player */
     libvlc_media_player_release (mp);
 
     libvlc_release (inst);
 
     return 0;
 }