Simple Audio playing sample with LibVLC
In this blog i will demonstrate how to play mp3 file using simple LibVLC code.
- First you need to install LibVLC into your system. if you not, follow how to install tutorial to install LibVLC.
- Create new audio.c file on your home directory in ubuntu with below content.
#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, "/home/audio.mp3");
// 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(100);//play the audio 100s
// stop playing
libvlc_media_player_stop(mp);
// free the media_player
libvlc_media_player_release(mp);
libvlc_release(inst);
return 0;
}
- you need place mp3 file named audio.mp3 in your home directory.
- then navigate to the directory where you save audio.c and simply hit this to compile the audio.c file.
gcc $(pkg-config --cflags libvlc) -c audio.c -o audio.o
gcc audio.o -o audio $(pkg-config --libs libvlc)
- Hit this on the ubuntu terminal to run your audio playing example.
./audio
Add Comment
Comments (0)