最近使用vlc-qt做播放器,效果比较好,可以播放本地文件与rtmp流,特别rtmp流的播放,由于vlc库的延迟,可能会有2s左右的延迟
1 vlc库的下载,下载的vlc都不带sdk目录了,所以现在下来的安装文件不能用来开发,推荐地址http://download.videolan.org/pub/videolan/vlc/ 推荐下载3.7.1版本的,之前下了个2.x的版本库有问题,导致编译出错(坑)
2 vlc-qt下载,vlc-qt是使用libvlc的库写了可以直接播放视频的widget,git地址https://github.com/vlc-qt/vlc-qt
下载:
git clone git://github.com/vlc-qt/vlc-qt.git
git submodule init
git submodule update
3 使用cmake编译
在vlc-qt下新建build目录,用于cmake配置输出,
点击Config按钮,配置编译器
配置完成后点击finish,会自动config
如果没出错,恭喜,点击Generate Projection即可
我这里出错,
By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5Core", but CMake did not find one.
是找不到Qt5Core.lib 的cmake文件,可以手动指定
其他的lib也是一样,其中有libvlcCore与libvlc以及libvlc的 include目录要注意,在之前下载的vlc文件夹中,我的如下图
configure完成后,点击Generate,生成vs工程,生成完成后点击open project即可
打开工程,只需生成core与widgets模块即可
生成完成后,在buid/src/core 与buid/src/widgets下有生成的lib与dll,至此,vlc-qt编译完成,里面代码示例TestWidgetsPlayer也可以编译运行看一下,对于使用很有帮助,基本按照TestWidgetsPlayer示例即可,我编译好的分享给大家https://download.csdn.net/download/i7891090/11946361
4 播放器编写
.h文件
#pragma once #include <QWidget> #include "ui_LocalPlayer.h"
class VlcInstance; class VlcMedia; class VlcMediaPlayer;
class LocalPlayer:public QWidget { Q_OBJECT public: explicit LocalPlayer(QWidget *parent = nullptr); ~LocalPlayer(); void StartPlay(const QString &strFile);
private:
private slots : void OnPlayEnd(); void OnPauseClicked(bool); void OnSgnVideoPaused(); void OnSgnVideoPlaying(); private: Ui::LocalPlayer *ui; VlcInstance *_instance; VlcMedia *_media; VlcMediaPlayer *_player; QString _strFileName; };
.cpp文件
#include "VlcLocalPlayer.h" #include <QDesktopWidget> #include <QTcpSocket> #include <QByteArray>
VlcLocalPlayer::VlcLocalPlayer(QWidget *parent) : QWidget(parent) , ui(nullptr) , m_backWdt(nullptr) , m_pisocket(nullptr) { ui = new Ui::VlcLocalPlayerClass; ui->setupUi(this); m_backWdt = new QWidget; this->setWindowIcon(QIcon("./app.ico")); m_backWdt->setWindowIcon(QIcon("./app.ico")); //保存在最顶层 setWindowFlags(this->windowFlags()|Qt::WindowStaysOnTopHint); InitServer(PORT); }
VlcLocalPlayer::~VlcLocalPlayer() { delete ui; m_svr.close(); if (m_backWdt != nullptr) { delete m_backWdt; m_backWdt = nullptr; } }
void VlcLocalPlayer::StartPlay(const QString & strFile) { m_backWdt->show(); m_backWdt->setGeometry(0,0,QApplication::desktop()->width(), QApplication::desktop()->height()); m_backWdt->setWindowOpacity(0); if (!strFile.isEmpty()) { ui->widget->StartPlay(strFile); } }
void VlcLocalPlayer::OnCloseClicked() { //this->close(); m_backWdt->close(); }
void VlcLocalPlayer::closeEvent(QCloseEvent *event) { m_backWdt->close(); this->close(); }
bool VlcLocalPlayer::InitServer(int nPort) { if (!m_svr.listen(QHostAddress::Any,nPort)) { return false; } connect(&m_svr, SIGNAL(newConnection()), this, SLOT(newClientConnect())); return true; }
void VlcLocalPlayer::newClientConnect() { m_pisocket = m_svr.nextPendingConnection(); bool ret = connect(m_pisocket,SIGNAL(readyRead()),this,SLOT(OnSocketReadyRead())); ret = connect(m_pisocket,SIGNAL(disconnect()),this,SLOT(OnScokDisconnect())); m_pisocket->waitForReadyRead(); }
void VlcLocalPlayer::OnSocketReadyRead() { QByteArray buf; buf = m_pisocket->readAll(); qDebug() << buf; if (buf == "close") { m_svr.close(); m_backWdt->close(); this->close(); } }
void VlcLocalPlayer::OnScokDisconnect() { //TODO }
源码分享给大家https://download.csdn.net/download/i7891090/11946474