My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

LibVLC basic APIs

Omar Ahmed's photo
Omar Ahmed
·May 29, 2020

Introduction

This document is meant to highlight some of the important classes and interfaces available in the libVLC source-base and how to use them efficiently in your project. This manual is not intended to explain what libVLC is, and what libVLC code looks like. It assumes that you know the basics of coding and interested in making a multimedia project using libVLC. If you think there should be a method that is not listed, check it in the doxygen sources.

Important and useful APIs

Before you can use the API, you must include vlc/vlc.h in your program.

libvlc_new

Every program that uses libVLC API must first initialize a new instance of VLC using libvlc_new() function. The function returns NULL in case of an error so make sure to check the success of initialization before using the VLC instance in your program.

Example
libvlc_instance_t *vlc_instance;
vlc_instance = libvlc_new(0, NULL);

if(vlc_instance == NULL) {
        printf("There was an error initializing VLC\n");
        exit(1);
} else {
        printf("VLC initialized successfully\n");
}

libvlc_media_new_path

We will need to use a media file ( mp4, mp3, mpeg..etc ) in our program. libvlc_media_new_path function is used to specify a media file which will be used. The function takes an absolute path as input. Also The function returns NULL in case of failure in getting the media file so make sure to check the media file instance before using it.

Example
libvlc_media_t *my_media_file;
my_media_file = libvlc_media_new_path(vlc_instance, "/tmp/test.mp4");

if(my_media_file == NULL) {
        printf("There was an error opening this media file\n");
        exit(1);
} else {
        printf("media file opened successfully\n");
}

libvlc_media_player_new_from_media

To play the media file, we will need a media player. The media player could be created using libvlc_media_player_new_from_media() function. The function returns NULL in case of failure in creating the media player so make sure to check the media player instance before using it.

Example
libvlc_media_player_t *my_player;
my_player = libvlc_media_player_new_from_media(my_media_file);

if(my_player == NULL) {
        printf("There was an error creating the media player\n");
        exit(1);
} else {
        printf("media player created successfully\n");
}

libvlc_media_player_play

After making a media player, and specify our media file we want to play our media player. libvlc_media_player_play() function is used to play the media player. The media player will play asynchronously. Therefore, unless we use sleep() function after playing the media player, it will close immediately.

Example
// Play the media player immediately.
libvlc_media_player_play(my_player);

// Play the media player for 30 seconds then close.
sleep(30);

libvlc_media_release

At the end of our program, or whenever we will not use our media file again. we should free the memory reserved for our media file. libvlc_media_release() function is used to free media file memory. We shouldn't use media file instance after releasing it.

Example
libvlc_media_release(my_media_file);

Other Useful Resources