使用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

智能推荐

window绕开UAC——利用UAC白名单程序提权_边缘计算机的博客-程序员秘密

Se在实际开发过程中,可以通过Process Explorer检查服务或程序处于哪个Session,会不会遇到Session 0 隔离问题。我们在Services 中找到之前加载的AlertService 服务,右键属性查看其Session 状态。

Springboot+ solr 快速入门指南_时间都哪去了的博客-程序员秘密

Solr基于Lucene的Java搜索引擎服务器Apache Lucene项目的开源企业搜索平台。其主要功能包括全文检索、命中标示、分面搜索、动态聚类、数据库集成,以及富文本(如Word、PDF)的处理。Solr是高度可扩展的,并提供了分布式搜索和索引复制。Solr是最流行的企业级搜索引擎,Solr4 还增加了NoSQL支持。Solr安装配置 1.下载Solr安装包 wgethttps://downloads.apache.org/lucene/solr/8.5.2/solr-8.5...

<<深入浅出MFC>>笔记1——Win32基本程序观念_win32程序观念_iot-genius的博客-程序员秘密

1.Windows SDK程序开发流程主要分为程序代码和UI资源两部分。 2.以消息未基础,以事件驱动    程序不断的等待外围的输入,判断在处理。操作系统通过捕捉外围输入,以消息的形式进入程序中,程序通过获取的不同消息进行不同的处理。USER模块掌管外围的驱动程序。    程序通过一个循环来获取消息。MSG msg;

程序员日记_程序员日志_chenyingchun的博客-程序员秘密

  人都说男人三十而立。以前还感觉不到,总发觉自己离这个三十还有段距离,这段时间的经历慢慢的体会到了。是啊 男人三十谁不想立阿?  以前自己总没有从过去的影子里面走出来,没有好好的审视自己.人要适应角色的变换。很多时候我都发觉自己都已经变老了,是否还能再写程序?下班以后感觉再也没有激情写点东西了,手提也变成了游戏机。感觉以前的系统慢慢的离我远去,连女朋友都说我对编程缺乏热情了!   糟糕!!!网站

Vulnstack(一)_内网渗透_iO快到碗里来的博客-程序员秘密

0x00 Preface网上有很多关于 Vulnstack(一) 的优质文章,本篇文章仅用于记录笔者自身的学习过程。0x01 环境准备共有三台机器,Win7是对外的web机,win2003和win2008是内网机器。VM1(Win7)添加一块网卡,分别设置为:VMnet1(仅主机模式)、VMnet8(NAT模式)VM2(Win2003)网卡设置为:VMnet1(仅主机模式)VM2(Win2008)网卡设置为:VMnet1(仅主机模式)打开Win2003、Win2008,输入默认

计算机在材料科学中的模拟方法,计算机在材料科学中应用 - 计算机模拟 部分 -..._因特耐特王的博客-程序员秘密

计算机在材料科学中应用 - 计算机模拟 部分 -View149Download0Embed Size (px)344 x 292429 x 357514 x 422599 x 487DESCRIPTION计算机在材料科学中应用 - 计算机模拟 部分 -. 任课教师: 杨弋涛 上海大学材料学院材料工程系. 教学背景. CAD, CAE, CAM 是材料科学领域的技术前沿和活跃的研究领域。...

随便推点

arm-linux-gcc交叉编译器和gcc编译器的下载地址_缘客_ql的博客-程序员秘密

转载:https://blog.csdn.net/chuanzhilong/article/details/52831511arm-linux-gccarm-none-linux-gnueabi-gcc编译器arm-none-linux-gnueabi-gcc是 Codesourcery 公司(目前已经被Mentor收购)基于GCC推出的的ARM交叉编译工具。可用于交叉编译ARM系统中所有...

JavaWeb之JSTL_m0_50620266的博客-程序员秘密

JSTL什么是JSTL完成业务逻辑使用前需要导入jar包if 没有elsefor普通forc:forEach参数有 begin ,end ,step(步长),var循环变量(相当于i)test,对条件进行计算,还可以将条件的结果存入作用域中 JSTL 什么是JSTL 完成业务逻辑 使用前需要导入jar包 if 没有else test,对条件进行计算,还可以将条件的结果存入作用域中 &lt;c:if test="${10..

Eclipse+git中merge代码时出现conflict(冲突)的问题解决方案_eclipse merge冲突_wanyangnumberone的博客-程序员秘密

多人合作开发一个项目时,Eclipse中使用git pull更新代码时经常会出现conflict(冲突)的问题,主要是由于多人操作同一个文件导致的。使用git也没多久,今天终于摸索出一套方法可以解决该问题,主要操作步骤如下:1.需要提交代码之前,先从服务器上pull出最新代码,此时出现conflict(冲突),右击项目->Team->Synchronize Workspace,如下图:

Linux 安装mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz_流星天道酬勤的博客-程序员秘密

11.下载mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz的安装包云盘下载链接:https://pan.baidu.com/s/1CwH3yKNoQ_dUX4Q2xrA5Aw提取码:jxbk2.解压mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz# tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz如果报如下错误,请安装组件[[email protected]

【tensorflow 大马哈鱼】 tensorboard可视化《莫凡》_飞翔的大马哈鱼的博客-程序员秘密

 在当前目录下调用, 一个参数logdir, log是文件夹$ tensorboard --logdir log标准过程 #存储变量tf.summary.histogram('h_out', l1)#存储losstf.summary.scalar('loss', loss) #operation to merge all summary 建立merge操作#主要wr...

Selection-程序员秘密

antv x6 selection、

推荐文章

热门文章

相关标签