1. 程式人生 > >使用libvlc簡單的播放一首本地音樂檔案

使用libvlc簡單的播放一首本地音樂檔案


#include <iostream>
#include <unistd.h>

#include "vlc/vlc.h"  
#include "libvlc.h"
#include "libvlc_media_player.h"

#include "log_c.h"

//#define url_temp "/share/music/20Hz-stero.wav"
#define url_temp  "/share/music/1KHz-stero.wav"
int main()
{
	libvlc_instance_t *vlcInstance;
    libvlc_media_player_t *vlcPlayer;
	libvlc_media_t* 		   vlcMedia;
	std::string  url(url_temp);

	LOG_ERROR(("log_c.h"));

	 vlcPlayer = NULL;

    /* Initialize libVLC */
    vlcInstance = libvlc_new(0, NULL);
    /* Complain in case of broken installation */
    if (vlcInstance == NULL)
	{
       LOG_ERROR(("  libVLC player  Could not init libVLC"));
       exit(1);
    }

	if (vlcPlayer && libvlc_media_player_is_playing(vlcPlayer))
	{
		//stop
		 libvlc_media_player_stop(vlcPlayer);

        /* release the media player */
        libvlc_media_player_release(vlcPlayer);

        /* Reset application values */
        vlcPlayer = NULL;
       // slider->setValue(0);
     //   playBut->setText("Play");
        LOG_ERROR(("(vlcPlayer && libvlc_media_player_is_playing(vlcPlayer))"));
	}	

	/* Create a new Media */
	vlcMedia = libvlc_media_new_path(vlcInstance, url.c_str());
	if (!vlcMedia)
	{
		 LOG_ERROR(("	if (!vlcMedia)"));
          return 0;
	}
	/* Create a new libvlc player */
	vlcPlayer = libvlc_media_player_new_from_media (vlcMedia);

	/* Release the media */
	libvlc_media_release(vlcMedia);

	/* Integrate the video in the interface */
	/* And start playback */
	libvlc_media_player_play (vlcPlayer);

	LOG_ERROR(("libvlc_media_player_play"));

	while(2)
	{
		sleep(0xff);
	}
	return 0;
}

makefile

CXX = g++
CFLAGS = -Wall -g -std=c++11 -pthread
PWD_OUT := /share/demo_vlc
PWD_SRC := /share/demo_vlc
CICLUDES_PWD := -I /usr/include/vlc -I /usr/include/
CSO_PWD := /usr/lib
libname1 := -lvlc

OBJS := $(PWD_OUT)/vlc_demo.o
#OBJS += $(PWD_OUT)/YD_LEDManage.o

SRCS := $(PWD_SRC)/vlc_demo.cpp
#SRCS += $(PWD_SRC)/YD_LEDManage.cpp


TARGET = demo_vlc_app
$(TARGET):$(OBJS)
	$(CXX) $(CFLAGS) -o $(TARGET) $(OBJS) -L$(CSO_PWD) $(libname1) $(CICLUDES_PWD)
	@echo "=======finish========="
$(PWD_OUT)/%.o:%.cpp
	$(CXX) $(CFLAGS) -c $(SRCS) $(CICLUDES_PWD)

clean:
	rm $(OBJS)  $(TARGET) -rf
	@echo "=============clean ok========"