A simple C program to play mp3 using libvlc

2019-02-05 07:29发布

问题:

I am an average C/C++ programmer. Recently I took a project to make a media player with a smart playlist that will work like Zune's SmartDj. I have decided to use libvlc for playing.

I have never coded an open source software before, so I know nothing about git and all. Can you please help me to write at least a C program to play a mp3 file?

Where should I get started? How do you extract a song's artist and other information from the mp3 file itself?

regards.

回答1:

be sure that you have installed the following packages (else install it):

$ apt-get install libvlccore-dev libvlc-dev

test.c:

#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_path(inst, "path to MP3 file");

    // create a media play playing environment
    mp = libvlc_media_player_new_from_media(m);

    // no need to keep the media now
    libvlc_media_release(m);

    // play the media_player
    libvlc_media_player_play(mp);

    sleep(10);

    // stop playing
    libvlc_media_player_stop(mp);

    // free the media_player
    libvlc_media_player_release(mp);

    libvlc_release(inst);


    return 0;
}

how to link and compile:

$ gcc $(pkg-config --cflags libvlc) -c test.c -o test.o

$ gcc test.o -o test $(pkg-config --libs libvlc)