使用libvlc播放音乐,实时获取位置、播放状态、时长,播放httpURL_libvlc 状态_zhouzhenhe2008的博客-程序员秘密

技术标签: libvlc  

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

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

#include "log_c.h"
#include "VlcMusicPlayer.h"

#define url_temp "/share/music/1KHz-stero.wav"
#define http_url  "http://101.132.74.49:8080/bcch.wav"

int main(int argc, char **argv)
{

	VlcMusicPlayer   VlcMusicPlayerHandle;
	int i =0;
	for(i = 0; i < argc ;i++ )
	{
		LOG_ERROR(("%d argv %s",i, argv[i]));

	}

	if(std::string(argv[1]) == std::string("http"))
	{

		std::string  url(http_url);
		LOG_ERROR(("=%s", url.c_str()));
		VlcMusicPlayerHandle.openUrl( url);
	}
	else
	{
		std::string  url(url_temp);
		LOG_ERROR(("=%s", url.c_str()));
		VlcMusicPlayerHandle.openFile( url);
	}
	

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




VlcMusicPlayer.h

/******************************
 * Qt player using libVLC     *
 * By protonux                *
 *                            *
 * Under WTFPL                *
 ******************************/

#ifndef VlcMusicPlayer_H
#define VlcMusicPlayer_H

#include <iostream>

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

class VlcMusicPlayer 
{
        public:
               VlcMusicPlayer();
               virtual ~VlcMusicPlayer();

        public:
			   int init();
               void  openFile(const std::string & file_string);
			   void   openUrl(const std::string & url);
               void play();
               void stop();
               void mute();
               int  changeVolume(int);
               void changePosition(int);
               void updateInterface();
			   void timer(int ms);
			   void StartTimer();
			   void StopTimer();
			   #if 0
			   void music_register();
			   void music_unregister();
			   void   OnEndReached( const struct libvlc_event_t *p_event, void *p_data );
               void   OnPositionChanged( const struct libvlc_event_t *p_event, void *p_data );
			   	#endif
        private:
               libvlc_instance_t 		*vlcInstance;
               libvlc_media_player_t 	*vlcPlayer;
			   libvlc_media_t 			*vlcMedia ;
			   libvlc_event_manager_t   *vlc_evt_man;
			   long long                m_s64Duration;
			   int                      m_nCurPos;
			   bool                     m_isMuteFlag;
			   int 						m_cur_vol;
			   libvlc_state_t  			m_emPlayState;

			   
};


#endif


VlcMusicPlayer.cpp

#include <unistd.h>
#include <thread>

#include "VlcMusicPlayer.h"
#include "log_c.h"

static int  g_sStopTimerFlag = 0; 

#define    TimerInternal     (300)

VlcMusicPlayer::VlcMusicPlayer()
{
    vlcPlayer = NULL;
	vlcInstance = NULL;
 	vlcMedia = NULL ;
	vlc_evt_man = NULL;
	m_s64Duration = -1;
	m_nCurPos = -1;
	m_isMuteFlag = false;

	m_cur_vol = 0;
	m_emPlayState = libvlc_NothingSpecial;
    int ret = init();
	if(0 != ret)
	{
		 LOG_ERROR((" VlcMusicPlayer  init   error!"));
		 return ;
	}
}

VlcMusicPlayer::~VlcMusicPlayer() 
{
    /* Release libVLC instance on quit */
	LOG_ERROR((" "));
	stop();
    if (vlcInstance)
    {
		libvlc_release(vlcInstance);
    }
}

int VlcMusicPlayer::init()
{
	static bool   sInitFlag = false;

	if(sInitFlag == true)
	{
		LOG_ERROR((" libVLC player  do not  repeat init libVLC"));
		return 0;
	}
	
     /* Initialize libVLC */
    vlcInstance = libvlc_new(0, NULL);

    /* Complain in case of broken installation */
    if (vlcInstance == NULL) 
	{
        LOG_ERROR((" libVLC player  Could not init libVLC"));
        return -1;
    }

	
	sInitFlag= true ;

	LOG_ERROR((" libVLC player  init libVLC success "));
	return 0;
    /* Interface initialization */
}

void VlcMusicPlayer::openFile(const std::string & file_string)
{

    /* The basic file-select box */
    //url  to utf-8 url
    
    /* Stop if something is playing */
    if (vlcPlayer && libvlc_media_player_is_playing(vlcPlayer))
    {
    	LOG_DEBUG(("libvlc_media_player_is_playing  ,to stop "));
        stop();
    }
	
    /* Create a new Media */
    vlcMedia = libvlc_media_new_path(vlcInstance, file_string.c_str());
    if (!vlcMedia)
    {
    	LOG_ERROR(("libvlc_media_new_path  error "));
        return;
    }
    /* Create a new libvlc player */
    vlcPlayer = libvlc_media_player_new_from_media (vlcMedia);
	if(NULL == vlcPlayer)
	{
		LOG_ERROR(("libvlc_media_player_new_from_media  error "));
		return;
	}
    /* Release the media */
    //libvlc_media_release(vlcMedia);

    /* And start playback */
    libvlc_media_player_play (vlcPlayer);

	StartTimer();
	
    /* Update playback button */
    //playBut->setText("Pause");
}

void VlcMusicPlayer::openUrl(const std::string & url)
{
	
		/* The basic file-select box */
		//url  to utf-8 url
		
		/* Stop if something is playing */
		if (vlcPlayer && libvlc_media_player_is_playing(vlcPlayer))
		{
			LOG_DEBUG(("libvlc_media_player_is_playing	,to stop "));
			stop();
		}
		
		/* Create a new Media */
		vlcMedia = libvlc_media_new_location(vlcInstance, url.c_str());
		if (!vlcMedia)
		{
			LOG_ERROR(("libvlc_media_new_location  error "));
			return;
		}
		/* Create a new libvlc player */
		vlcPlayer = libvlc_media_player_new_from_media (vlcMedia);
		if(NULL == vlcPlayer)
		{
			LOG_ERROR(("libvlc_media_player_new_from_media	error "));
			return;
		}
		/* Release the media */
		//libvlc_media_release(vlcMedia);
	
		/* And start playback */;
		libvlc_media_player_play (vlcPlayer);

		StartTimer();
		/* Update playback button */
		//playBut->setText("Pause");
}

void VlcMusicPlayer::play() {
    if (!vlcPlayer)
    {
    	LOG_ERROR(("vlcPlayer  is  NULL "));
        return;
    }
	
    if (libvlc_media_player_is_playing(vlcPlayer))
    {
        /* Pause */
		LOG_DEBUG(("NEXT  libvlc_media_player_pause()"));
        libvlc_media_player_pause(vlcPlayer);
       // playBut->setText("Play");
    }
    else
    {
        /* Play again */
		LOG_DEBUG(("NEXT  libvlc_media_player_play()"));
        libvlc_media_player_play(vlcPlayer);
       ///playBut->setText("Pause");
    }
}

int VlcMusicPlayer::changeVolume(int vol) { /* Called on volume slider change */

    if (vlcPlayer)
    {
    	int ret = 0;
		LOG_DEBUG(("NEXT  libvlc_audio_set_volume()  vol = %d", vol));
		ret =  libvlc_audio_set_volume (vlcPlayer,vol);

		m_cur_vol = vol;
		return ret;
		
    }
	
    return 0;
}

void VlcMusicPlayer::changePosition(int pos) { /* Called on position slider change */

    if (vlcPlayer)
    {
    	LOG_DEBUG(("NEXT  libvlc_media_player_set_position()  pos = %d", pos));
    	libvlc_media_player_set_position(vlcPlayer, (float)pos/1000.0);
    }
}

void VlcMusicPlayer::updateInterface() //timer
{ //Update interface and check if song is finished

    if (NULL !=vlcPlayer)
    {
    	/* update the timeline */
	    float pos = libvlc_media_player_get_position(vlcPlayer); //pos  is 0.01~0.99 1.0
		m_nCurPos = (int)(pos*1000.0);
	    /* Stop the media */
		libvlc_state_t state = libvlc_media_player_get_state(vlcPlayer);
		m_emPlayState = state;//libvlc_state_t  m_emPlayState
		LOG_DEBUG(("state{%d} m_nCurPos = %d",state, m_nCurPos));
	    if (state == libvlc_Ended)
	    {
	    	this->stop();
	    }
    }

	if(NULL != vlcMedia)
	{
		long long s64Duration = libvlc_media_get_duration( vlcMedia);	
		if(-1 != s64Duration)  //s64Duration/1000 s
		{
			m_s64Duration = s64Duration;

			long long min = s64Duration/1000/60;
			long long sec = s64Duration/1000%60;
			LOG_DEBUG(("m_s64Duration{%lld}  %lld:%lld",m_s64Duration , min,sec));
		}	
	}
	
}


void  VlcMusicPlayer::timer(int ms)
{
	while(1)
	{
		if(g_sStopTimerFlag == 0)
		{
			LOG_DEBUG(("break"));
			break;
		}
		
		updateInterface();

		usleep(1000*ms);// ms
	}
}

void VlcMusicPlayer::StartTimer()
{
	g_sStopTimerFlag = 1;
	LOG_DEBUG(("  "));

	std::thread   t(&VlcMusicPlayer::timer, this , TimerInternal);
	t.detach();
}

void VlcMusicPlayer::StopTimer()
{
	g_sStopTimerFlag = 0;
	LOG_DEBUG(("  "));
}

void VlcMusicPlayer::stop() {

	LOG_DEBUG(("  "));
	StopTimer();
	if(vlcMedia)
	{
		/* Release the media */
		libvlc_media_release(vlcMedia);
		vlcMedia = NULL;
	}
	
    if(vlcPlayer) {
        /* stop the media player */
        libvlc_media_player_stop(vlcPlayer);

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

        /* Reset application values */
        vlcPlayer = NULL;
		m_emPlayState = libvlc_NothingSpecial;
		m_nCurPos = 0;
       // slider->setValue(0);
        //playBut->setText("Play");
    }
}

void VlcMusicPlayer::mute() {
    if(vlcPlayer) {
        if(m_isMuteFlag == true) { //if already muted...

                this->changeVolume(80);
				m_cur_vol = 80;
				m_isMuteFlag = false;
                //volumeSlider->setValue(80);

        } else { //else mute volume

				m_isMuteFlag = true;
				m_cur_vol = 0;
                this->changeVolume(0);
                //volumeSlider->setValue(0);

        }
    }
}

#if 0
void  VlcMusicPlayer::music_register()
{
#if 0
libvlc_event_manager_t *
libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
{
    return &p_mi->event_manager;
}
typedef void ( *libvlc_callback_t )( const struct libvlc_event_t *p_event, void *p_data );
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached, ::OnEndReached_VLC, NULL);

#endif
	if(NULL == vlcPlayer)
	{
		LOG_ERROR(("vlcPlayer  is  NULL "));
		return ;
	}
	
	vlc_evt_man = libvlc_media_player_event_manager(vlcPlayer);
    libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached,
		(libvlc_callback_t) (&VlcMusicPlayer::OnEndReached), NULL);
	
    libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, 
		(libvlc_callback_t)(&VlcMusicPlayer::OnPositionChanged), NULL);

	return ;
}

void  VlcMusicPlayer::music_unregister()
{

}

void VlcMusicPlayer::OnEndReached( const struct libvlc_event_t *p_event, void *p_data )
{
	LOG_ERROR((" "));
	this->stop();
}

void VlcMusicPlayer:: OnPositionChanged( const struct libvlc_event_t *p_event, void *p_data )
{
	if(NULL == vlcPlayer)
	{
		LOG_ERROR(("vlcPlayer  is  NULL "));
		return ;
	}
	float factor = libvlc_media_player_get_position(vlcPlayer);
	LOG_ERROR((" factor [%.2f] ",factor));
	
}

#endif



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)/VlcMusicPlayer.o


SRCS := $(PWD_SRC)/vlc_demo.cpp
SRCS += $(PWD_SRC)/VlcMusicPlayer.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========"



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhouzhenhe2008/article/details/77828363

智能推荐

电子发票XML样式_七月在野的博客-程序员秘密

电子发票XML样式文章目录电子发票XML样式一、航信1、专普票2、电子发票二、百望、UKey1、专普票2、电子发票提示:以下是各开票软件导入xml开票格式一、航信1、专普票格式如下:&lt;?xml version="1.0" encoding="GBK"?&gt;&lt;Kp&gt;&lt;Version&gt;2.0&lt;/Version&gt;&lt;Fpxx&gt;&lt;Zsl&gt;1&lt;/Zsl&gt;&lt;Fpsj&gt;&lt;Fp&gt;&lt;Djh&gt

将一个数组正反交换(指针)_Begin to change的博客-程序员秘密

#include&amp;lt;stdio.h&amp;gt;void dx(int *a,int n); int main(){ int a[10],i,n,*p; for(i=0;i&amp;lt;10;i++) { scanf(&quot;%d&quot;,&amp;amp;a[i]); } scanf(&quot;%d&quot;,&amp;amp;n); dx(a,n); /* p=a; for(i=0;i&amp;lt;10;i++) { ...

通过acpi_device_hid 得到device的HID_devices_hid_tiantao2012的博客-程序员秘密

用acpi_device_hid 可以的到device的hid,如下所示:static int acpi_processor_get_info(struct acpi_device *device){//这里的#define ACPI_PROCESSOR_OBJECT_HID "LNXCPU" if (!strcmp(acpi_device_hid(device), ACPI_PROC

vue的路由模式_vue 路由模式_若枫~~的博客-程序员秘密

vue 的路由模式⼀共有两种,分别是哈希和 history.他们的区别是 hash 模式不会包含在 http 请求当中,并且 hash 不会重新加载⻚⾯,⽽使⽤ history 模式的话,如果前端的 url 和后端发起请求的 url 不⼀致的话,会报 404 错误,所以使⽤ history 模块的话我们需要和后端进⾏配合. history 的原理就是利⽤ html5 新增的两个特性⽅法,分别是 pushState 和 replaceState 来完成的.........

无法访问windows server 2008共享文件夹_server2008共享不能访问_棠宁的博客-程序员秘密

在访问windows server 2008共享文件夹时提示“windows 无法访问 \2008r2的IP 请检查名称的拼写。否则,网络可能有问题……”按照网上教程开启网络发现开启以下三个服务:1.Function Discovery Resource Publication2.SSDP Discovery3.UPnP Device Host经上述操作后,仍未解决问题。随后t...

随便推点

您真的了解全电发票吗?_授信额度依职权调整_Michael_张洲福的博客-程序员秘密

全电发票的全称是:全面数字化的电子发票,是与纸质发票具有同等法律效力的全新发票,不以纸质形式存在、不用介质支撑、无须申请领用、发票验旧及申请增版增量。纸质发票的票面信息全面数字化,将多个票种集成归并为电子发票单一票种,全电发票实行全国统一赋码、自动流转交付。依托可信身份体系和电子发票服务平台开具的,以电子方式存储的收付款凭证。无纸化:以数据电文作为法律凭证、不需要纸质载体、没有印制环节;网络化:申请、领用、开具、流转、查验等流程都可以在互联网上进行,省去验旧购新环节,纳税人不再需要往返税务机关领取纸质发票;

你想要知道的“全电发票接口”_乐企接口_小白菜自媒体的博客-程序员秘密

*全电发票是国家税务总局随着金税四期推出的全国统一的电子发票服务平台,它不用申领专用税控设备和进行票种核定、系统自动赋予开具额度,并根据纳税人行为动态调整发票额度,按照全新管理流程,实现开业即可开票。全电发票开具后,发票数据文件自动发送到开票方和收票方的税务数字账户,便于交付入账,减少人工收发。同时依托税务数字账户,纳税人可对全量发票数据进行自动归集。**

pta c++ 程序填空题_c++程序填空题_prominent.949的博客-程序员秘密

pta c++ 判断题 类与对象 数据共享与保护 数组 指针 字符串 继承与派生 多态性 群体类和群体数据的组织 泛型程序设计 流类库与输入输出 异常处理 stl 面向对象程序设计 成员函数 构造函数 析构函数 类的组合 作用域 静态 动态 友元 常对象 多文件结构 对象数组 this指针 动态内存分配 引用 虚函数 抽象类 运算符重载 函数模板 类模板 容器

ubuntu18系统anaconda安装tensorflow——补充-程序员秘密

ubuntu18系统anaconda安装tensorflow_qq_39429669的博客-程序员秘密安装的过程主要思考两个问题:tensorflow与CUDA、cudnn的版本兼容,特别是分清上兼容还是下兼容。 虚拟环境(比如conda install tensorflow-gpu==1.2.1自动安装对应版本CUDA、cudnn的安装方式)安装的CUDA、cudnn是否可用,是否会和在系统安装的CUDA、cudnn冲突,如果采用这种方式,是否可以不用提前在系统中安装CUDA、cudnn。接

C语言中可变参宏的一种实现方式_奔跑的小刺猬的博客-程序员秘密

好久不写了,有点手生了都。下面主要分析可变参数宏的一种实现。因为C语言的标准库是可平台(处理器)有关的,所以本历程不能保证所有的处理器可用。本人在裸机ARM的处理器上实现过类似printf函数,适合ARM平台使用。下面带代码来自linux内核的stdarg.h头文件typedef char *va_list;    ​#define _AUPBND (sizeof (acpi_native_int...

推荐文章

热门文章

相关标签