使用 VLC-Qt 库实现流媒体及视频文件多画面显示程序,根据simple-player例子修改,使用VLC-Qt_1.0.1_win64_msvc2013版本。
GitHub - vlc-qt/examples: VLC-Qt Examples
https://github.com/vlc-qt/examples
Releases · vlc-qt/vlc-qt · GitHub
https://github.com/vlc-qt/vlc-qt/releases
有1x2、2x2、2x3、3x3多个选择,根据选择生成多个播放画面,3x3需要占用90%的CPU及500M以上内存,根据exe名称读取对应文本文件获得视频地址,需要播放的视频文件个数和能播放的画面个数需要进行取舍。在对音量进行控制的时候,由于VlcWidgetVolumeSlider控件需要关联到VlcMediaPlayer控件,实现的时候需要一个临时的播放器控件对音量控件进行接收。通过播放器控件的右键菜单控制播放功能,包括暂停、重新打开、音量选项。
提示:添加于2019年1月5号,想实现一个“除此之外全部静音”的功能,初始代码如下
void MainWindow::setOtherVolume0()
{
for (int i = 0; i < mVlcMediaLst.size(); ++i)
{
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(i));
ui->volume->setVolume(0);
}
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(100);
}
但是一直不能实现效果,后根据如下网址修改
VlcAudio - setVolume(int volume) · Issue #54 · vlc-qt/vlc-qt · GitHub
https://github.com/vlc-qt/vlc-qt/issues/54
修改后的代码如下,实现了效果。
void MainWindow::setOtherVolume0()
{
for (int i = 0; i < mVlcMediaLst.size(); ++i)
{
mVlcMediaPlayerLst.at(i)->audio()->setVolume(0);
}
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(100);
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFont font;
font.setPixelSize(14);
setFont(font);
mTitle = QStringLiteral("VLC-Qt 流媒体及视频文件多画面显示");
setWindowTitle(mTitle);
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
mTimer->start(1000);
ui->comboBox->setMaximumHeight(25);
ui->pushButton->setMaximumHeight(25);
ui->label->setMaximumHeight(25);
ui->comboBox->insertItem(0, "1x2");
ui->comboBox->insertItem(1, "2x2");
ui->comboBox->insertItem(2, "2x3");
ui->comboBox->insertItem(3, "3x3");
mVlcInstance = new VlcInstance(VlcCommon::args(), this);
mVlcMediaPlayerTmp = new VlcMediaPlayer(mVlcInstance);
ui->volume->setVisible(false);
ui->volume->setMediaPlayer(mVlcMediaPlayerTmp);
ui->volume->setVolume(0);
readUrls();
}
MainWindow::~MainWindow()
{
freeVlcLst();
delete mVlcMediaPlayerTmp;
if (mVlcInstance != nullptr)
{
delete mVlcInstance;
}
if (mTimer->isActive())
{
mTimer->stop();
}
delete mTimer;
delete ui;
}
void MainWindow::updateTimer()
{
QString dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
ui->label->setText(QStringLiteral("%1").arg(dateTime));
}
void MainWindow::readUrls()
{
mUrlLst.clear();
QFile file(QStringLiteral("%1.txt").arg(qApp->applicationDisplayName()));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
in.setCodec("UTF-8");
while (!in.atEnd())
{
QString str = in.readLine();
if (str.isEmpty() || str.startsWith("$$"))
{
continue;
}
mUrlLst.append(str.split("#")[1]);
}
}
void MainWindow::freeVlcLst()
{
ui->volume->setMediaPlayer(mVlcMediaPlayerTmp);
ui->volume->setVolume(0);
foreach (VlcMedia *media, mVlcMediaLst)
{
delete media;
}
foreach (VlcMediaPlayer *player, mVlcMediaPlayerLst)
{
delete player;
}
foreach (VlcWidgetVideo *widget, mVlcWidgetVideoLst)
{
delete widget;
}
mVlcMediaLst.clear();
mVlcMediaPlayerLst.clear();
mVlcWidgetVideoLst.clear();
}
void MainWindow::initVideoWidgets()
{
int count = 0;
QString str;
QStringList strLst;
for (int i = 0; i < mTotal; ++i)
{
strLst.append(QStringLiteral("%1").arg(i));
count++;
if (count % mColumn == 0)
{
str.append(strLst.join("|"));
str.append(",");
strLst.clear();
}
}
str.remove(str.length() - 1, 1);
foreach (QString row, str.split(","))
{
QHBoxLayout *layout = new QHBoxLayout;
foreach (QString column, row.split("|"))
{
layout->addWidget(mVlcWidgetVideoLst.at(column.toInt()));
}
ui->verticalLayout->addLayout(layout);
}
}
void MainWindow::on_pushButton_clicked()
{
freeVlcLst();
if (mIsPlaying)
{
ui->comboBox->setEnabled(true);
ui->pushButton->setText(QStringLiteral("开始播放"));
mIsPlaying = false;
}
else
{
ui->comboBox->setEnabled(false);
ui->pushButton->setText(QStringLiteral("停止播放"));
mIsPlaying = true;
for (int i = 0; i < mTotal; ++i)
{
VlcWidgetVideo *widget = new VlcWidgetVideo(this);
VlcMediaPlayer *vlcMediaPlayer = new VlcMediaPlayer(mVlcInstance);
vlcMediaPlayer->setVideoWidget(widget);
widget->setMediaPlayer(vlcMediaPlayer);
mVlcWidgetVideoLst.append(widget);
mVlcMediaPlayerLst.append(vlcMediaPlayer);
widget->setObjectName(QStringLiteral("%1").arg(i));
widget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(widget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(menuDisplayed(QPoint)));
}
initVideoWidgets();
int validSize = (mUrlLst.size() >= mTotal) ? mTotal : mUrlLst.size();
for (int i = 0; i < validSize; ++i)
{
QString url = mUrlLst.at(i);
QFileInfo file(url);
if (file.isFile())
{
VlcMedia *vlcMedia = new VlcMedia(url, true, mVlcInstance);
mVlcMediaLst.append(vlcMedia);
}
else
{
VlcMedia *vlcMedia = new VlcMedia(url, mVlcInstance);
mVlcMediaLst.append(vlcMedia);
}
}
for (int i = 0; i < validSize; ++i)
{
mVlcMediaPlayerLst.at(i)->open(mVlcMediaLst.at(i));
}
}
}
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
QStringList lst = arg1.split("x");
mRow = lst[0].toInt();
mColumn = lst[1].toInt();
mTotal = mRow * mColumn;
}
void MainWindow::menuDisplayed(const QPoint &pos)
{
Q_UNUSED(pos);
mChoosePlayerNum = sender()->objectName().toInt();
if (mChoosePlayerNum >= mUrlLst.size())
{
return;
}
QMenu *menu = new QMenu(this);
QAction *pause = new QAction(QStringLiteral("暂停"), this);
QAction *reOpen = new QAction(QStringLiteral("重新打开"), this);
QAction *volume100 = new QAction(QStringLiteral("音量100%"), this);
QAction *volume75 = new QAction(QStringLiteral("音量75%"), this);
QAction *volume50 = new QAction(QStringLiteral("音量50%"), this);
QAction *volume25 = new QAction(QStringLiteral("音量25%"), this);
QAction *volume0 = new QAction(QStringLiteral("音量0%"), this);
menu->addAction(pause);
menu->addSeparator();
menu->addAction(reOpen);
menu->addSeparator();
menu->addAction(volume100);
menu->addAction(volume75);
menu->addAction(volume50);
menu->addAction(volume25);
menu->addAction(volume0);
connect(pause, SIGNAL(triggered(bool)), this, SLOT(pause()));
connect(reOpen, SIGNAL(triggered(bool)), this, SLOT(reOpen()));
connect(volume100, SIGNAL(triggered(bool)), this, SLOT(volume100()));
connect(volume75, SIGNAL(triggered(bool)), this, SLOT(volume75()));
connect(volume50, SIGNAL(triggered(bool)), this, SLOT(volume50()));
connect(volume25, SIGNAL(triggered(bool)), this, SLOT(volume25()));
connect(volume0, SIGNAL(triggered(bool)), this, SLOT(volume0()));
menu->exec(QCursor::pos());
delete pause;
delete reOpen;
delete volume100;
delete volume75;
delete volume50;
delete volume25;
delete volume0;
delete menu;
}
void MainWindow::pause()
{
mVlcMediaPlayerLst.at(mChoosePlayerNum)->togglePause();
}
void MainWindow::reOpen()
{
mVlcMediaPlayerLst.at(mChoosePlayerNum)->open(mVlcMediaLst.at(mChoosePlayerNum));
}
void MainWindow::volume100()
{
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(100);
}
void MainWindow::volume75()
{
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(75);
}
void MainWindow::volume50()
{
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(50);
}
void MainWindow::volume25()
{
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(25);
}
void MainWindow::volume0()
{
ui->volume->setMediaPlayer(mVlcMediaPlayerLst.at(mChoosePlayerNum));
ui->volume->setVolume(0);
}