云起工作室 15711107967
libvlc
2025-02-06 10:42:40

下载地址:Index of /pub/videolan/vlc/

配置:(38条消息) LibVLC —— Qt下的配置(附所有出现的问题解决)_信必诺的博客-CSDN博客_qt配置libvlc

1、准备

    (1)默认已经安装好Qt。

    (2)在上述vlc官网下载vlc一般下载.7z文件,文件中包含有sdk。


2、打开vlc文件,分别将sdk、plugins拷出。在拷出的目录下创建bin文件夹,并将libvlccore.dll、libvlc.dll拷贝进去。

在工程根目录下创建vlc 目录,将sdk目录下的内容和plugins目录下的内容复制到vlc下,并在vlc下创建bin 文件夹并将libvlccore.dll、libvlc.dll拷贝进去。

3、创建Qt项目。

  (1)在pro文件下加入头文件、库的引用。

############################## VLC

INCLUDEPATH += vlc/include

LIBS += vlc/lib/libvlc.lib //可以用绝对路径

LIBS += vlc/lib/libvlccore.lib //可以用绝对路径

##############################


   (2)在Qt中加入vlc头文件,并打开改文件加入内容

#include "vlc/vlc.h"

// 打开"vlc/vlc.h"。在改文件内的"extern C"前面加入下面内容。

typedef __int64 ssize_t;

#include "vlc/vlc.h"


// 打开"vlc/vlc.h"。在改文件内的"extern C"前面加入下面内容。

typedef __int64 ssize_t;


(3)构建Qt目录,然后在exe目录下将libvlccore.dll、libvlc.dll拷贝进去、将plugins文件也拷贝进去。






#include <QMediaPlayer>

#include <QMediaPlaylist>


libvlc_instance_t* m_pInstance = libvlc_new(0,NULL); //创建实例

if(!m_pInstance){

qDebug() << "no vlc";

exit(EXIT_FAILURE);

}else{

libvlc_media_player_t* m_pMediaPlayer=libvlc_media_player_new(m_pInstance); //创建媒体

if(!m_pMediaPlayer){

libvlc_release(m_pInstance);

exit(EXIT_FAILURE);

}

}


//获取本地文件路径

QString filename=QFileDialog::getOpenFileName(this,"打开文件","D:/projects/test","视频文件(*.mp4 *.flv)");

if(filename.isEmpty()){

return;

}

filename.replace("/", "\\"); //必须

或者 filename = QDir::toNativeSeparators(filename);

//设置路径

libvlc_media_t* m_pMedia = libvlc_media_new_path(m_pInstance,filename.toStdString().c_str());

if(m_pMedia){

libvlc_media_parse(m_pMedia); // 有此可以获取时长

libvlc_media_player_set_media(m_pMediaPlayer,m_pMedia);

libvlc_media_player_set_hwnd(m_pMediaPlayer, (void*)(ui->video_widget->winId()) );

libvlc_media_release(m_pMedia);

m_pMedia=nullptr;

libvlc_media_player_play(m_pMediaPlayer);

}


//获取视频播放状态

libvlc_media_player_get_state(m_pMediaPlayer)==libvlc_state_t::libvlc_Playing ;正在播放

libvlc_media_player_get_state(m_pMediaPlayer)==libvlc_state_t::libvlc_Paused; 暂停状态

libvlc_media_player_get_state(m_pMediaPlayer)==libvlc_state_t::libvlc_Stopped; 停止状态


libvlc_media_player_pause(m_pMediaPlayer) ; //暂停

libvlc_media_player_stop(m_pMediaPlayer); // 停止


管理视频参数需要libvlc_event_manager_t 对象,在创建mediaplayer后,attach 视频seek 和声音调整的回调函数

if(m_pMediaPlayer){

libvlc_event_manager_t* m_pEvent_manager=libvlc_media_player_event_manager(m_pMediaPlayer);

libvlc_event_attach(m_pEvent_manager,libvlc_MediaPlayerPositionChanged,vlc_callback,this);

libvlc_event_attach(m_pEvent_manager,libvlc_MediaPlayerAudioVolume,vlc_callback,this);

}

void vlc_callback(const struct libvlc_event_t* p_event,void* p_data){

MainWidget* pMain = static_cast<MainWidget*>(p_data);

}


int duration =libvlc_media_get_duration(m_pMedia); //获取时长,单位毫秒,在释放前获取,这里不是媒体,


libvlc_media_player_get_postion(); //获取当前播放时长,百分比

libvlc_meida_player_set_postion(m_pMdiaPlayer,[value]/100.0);

libvlc_media_player_get_time(pMain->getMediaPlayer())/1000; //获取当前播放的时间单位毫秒

libvlc_media_player_get_volume(pMain->getMediaPlayer()); 获取当前播放的声音

libvlc_media_player_set_volume(m_pMediaPlayer,value); 设置播放声音




播放事件

pManager = libvlc_media_player_event_manager(pMediaPlayer);

libvlc_event_attach(pManager,libvlc_MediaPlayerPositionChanged,libvlc_callback,this);

libvlc_event_attach(pManager,libvlc_MediaPlayerAudioVolume,libvlc_callback,this);



多文件播放

pMediaListPlayer=libvlc_media_list_player_new(pInstance);

//监听事件

pManager = libvlc_media_list_player_event_manager(pMediaListPlayer);

libvlc_event_attach(pManager,libvlc_MediaPlayerPositionChanged,call_back,this);


pMediaList = libvlc_media_list_new(pInstance);

for(int i=0;i<filenames.size();i++){

QString filename = QDir::toNativeSeparators(filenames[i]);

pMedia = libvlc_media_new_path(pInstance,filename.toStdString().c_str());

libvlc_media_list_add_media(pMediaList,pMedia);


}


libvlc_media_list_player_set_media_list(pMediaListPlayer,pMediaList);

libvlc_media_list_player_set_media_player(pMediaListPlayer,pMediaPlayer);

libvlc_media_player_set_hwnd(pMediaPlayer,(void*)ui->media_widget->winId());

libvlc_media_list_player_play(pMediaListPlayer);