程序示例精选
C++ QT视频音乐播放器
如需安装运行环境或远程调试,可点击 博主头像进入个人主页查看博主联系方式,由专业技术人员远程协助!
前言
这篇博客针对《C++ QT视频音乐播放器》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。运行结果
文章目录
一、所需工具软件二、使用步骤
1. 代码实现
2. 运行结果
三、在线协助
一、所需工具软件
1. VS2019, C++2. OpenCV
二、使用步骤
代码如下(示例):
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPainter>
#include <QFileDialog>
#include <QDebug>
#include <QMouseEvent>
#include <QMessageBox>
#include "AppConfig.h"
#include "Base/FunctionTransfer.h"
#include "Widget/SetVideoUrlDialog.h"
#include "Widget/mymessagebox_withTitle.h"
Q_DECLARE_METATYPE(VideoPlayerState)
MainWindow::MainWindow(QWidget *parent) :
DragAbleWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this->getContainWidget());
FunctionTransfer::init(QThread::currentThreadId());
///初始化播放器
VideoPlayer::initPlayer();
setWindowFlags(Qt::FramelessWindowHint);//|Qt::WindowStaysOnTopHint); //使窗口的标题栏隐藏
// setAttribute(Qt::WA_TranslucentBackground);
//因为VideoPlayer::PlayerState是自定义的类型 要跨线程传递需要先注册一下
qRegisterMetaType<VideoPlayerState>();
mPopMenu = new QMenu(this);
mAddVideoAction = new QAction(QIcon("images/open.png"), QStringLiteral("打开网络流"), this);
mEditVideoAction = new QAction(QIcon("images/open.png"), QStringLiteral("修改数据"), this);
mDeleteVideoAction = new QAction(QIcon("images/open.png"), QStringLiteral("删除"), this);
mClearVideoAction = new QAction(QIcon("images/open.png"), QStringLiteral("清空"), this);
mPopMenu->addAction(mAddVideoAction);
// mPopMenu->addAction(mEditVideoAction);
// mPopMenu->addSeparator(); //添加分离器
mPopMenu->addAction(mDeleteVideoAction);
mPopMenu->addAction(mClearVideoAction);
connect(mAddVideoAction, &QAction::triggered, this, &MainWindow::slotActionClick);
connect(mEditVideoAction, &QAction::triggered, this, &MainWindow::slotActionClick);
connect(mDeleteVideoAction, &QAction::triggered, this, &MainWindow::slotActionClick);
connect(mClearVideoAction, &QAction::triggered, this, &MainWindow::slotActionClick);
connect(ui->pushButton_open, &QPushButton::clicked, this, &MainWindow::slotBtnClick);
connect(ui->toolButton_open, &QPushButton::clicked, this, &MainWindow::slotBtnClick);
connect(ui->pushButton_clear,&QPushButton::clicked, this, &MainWindow::slotBtnClick);
connect(ui->pushButton_play, &QPushButton::clicked, this, &MainWindow::slotBtnClick);
connect(ui->pushButton_volume, &QPushButton::clicked, this, &MainWindow::slotBtnClick);
connect(ui->horizontalSlider, SIGNAL(sig_valueChanged(int)), this, SLOT(slotSliderMoved(int)));
connect(ui->horizontalSlider_2, SIGNAL(valueChanged(int)), this, SLOT(slotSliderMoved(int)));
ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->listWidget, &QListWidget::itemDoubleClicked, this, &MainWindow::slotItemDoubleClicked);
connect(ui->listWidget, &QListWidget::customContextMenuRequested, this, &MainWindow::slotCustomContextMenuRequested);
ui->page_video->setMouseTracking(true);
ui->widget_videoPlayer->setMouseTracking(true);
// ui->page_video->installEventFilter(this);
ui->widget_videoPlayer->installEventFilter(this);
ui->widget_container->installEventFilter(this);
mCurrentIndex = 0;
mCurrentItem = nullptr;
mIsNeedPlayNext = false;
mPlayer = new VideoPlayer();
mPlayer->setVideoPlayerCallBack(this);
mTimer = new QTimer; //定时器-获取当前视频时间
connect(mTimer, &QTimer::timeout, this, &MainWindow::slotTimerTimeOut);
mTimer->setInterval(500);
mTimer_CheckControlWidget = new QTimer; //用于控制控制界面的出现和隐藏
connect(mTimer_CheckControlWidget, &QTimer::timeout, this, &MainWindow::slotTimerTimeOut);
mTimer_CheckControlWidget->setInterval(2500);
mAnimation_ControlWidget = new QPropertyAnimation(ui->widget_controller, "geometry");
ui->stackedWidget->setCurrentWidget(ui->page_open);
ui->pushButton_pause->hide();
resize(1024,768);
setTitle(QStringLiteral("我的播放器-V%1").arg(AppConfig::VERSION_NAME));
mVolume = mPlayer->getVolume();
std::thread([=]
{
while (1)
{
AppConfig::mSleep(500);
static QPoint lastPoint = QPoint(0, 0);
FunctionTransfer::runInMainThread([=]()
{
QPoint point = QCursor::pos();
if (this->geometry().contains(point))
{
if (lastPoint != point)
{
mTimer_CheckControlWidget->stop();
mTimer_CheckControlWidget->start();
lastPoint = point;
}
}
});
}
}).detach();
}
MainWindow::~MainWindow()
{
qDebug()<<__FUNCTION__;
AppConfig::saveConfigInfoToFile();
AppConfig::removeDirectory(AppConfig::AppDataPath_Tmp);
delete ui;
}
void MainWindow::showOutControlWidget()
{
mAnimation_ControlWidget->setDuration(800);
int w = ui->widget_controller->width();
int h = ui->widget_controller->height();
int y = ui->widget_container->height() - ui->widget_controller->height();
if (ui->widget_controller->isHidden())
{
ui->widget_controller->show();
mAnimation_ControlWidget->setStartValue(ui->widget_controller->geometry());
}
else
{
mAnimation_ControlWidget->setStartValue(ui->widget_controller->geometry());
}
// mAnimation_ControlWidget->setKeyValueAt(0, QRect(0, 0, 00, 00));
// mAnimation_ControlWidget->setKeyValueAt(0.4, QRect(20, 250, 20, 30));
// mAnimation_ControlWidget->setKeyValueAt(0.8, QRect(100, 250, 20, 30));
// mAnimation_ControlWidget->setKeyValueAt(1, QRect(250, 250, 100, 30));
mAnimation_ControlWidget->setEndValue(QRect(x, y, w, h));
mAnimation_ControlWidget->setEasingCurve(QEasingCurve::Linear); //设置动画效果
mAnimation_ControlWidget->start();
}
void MainWindow::hideControlWidget()
{
mAnimation_ControlWidget->setTargetObject(ui->widget_controller);
mAnimation_ControlWidget->setDuration(300);
int w = ui->widget_controller->width();
int h = ui->widget_controller->height();
int x = 0;
int y = ui->widget_container->height() + h;
mAnimation_ControlWidget->setStartValue(ui->widget_controller->geometry());
mAnimation_ControlWidget->setEasingCurve(QEasingCurve::Linear); //设置动画效果
mAnimation_ControlWidget->start();
}
void MainWindow::setVideoNums(const int &nums)
{
ui->label_num->setText(QStringLiteral("%1个文件").arg(nums));
}
void MainWindow::addVideoFiles(const QStringList &videoFileList)
{
if (!videoFileList.isEmpty())
{
QString filePath = videoFileList.first();
AppConfig::gVideoFilePath = QFileInfo(filePath).absoluteDir().path();
AppConfig::saveConfigInfoToFile();
}
for (QString filePath : videoFileList)
{
addVideoFile(filePath);
}
}
void MainWindow::addVideoFile(const QString &filePath)
{
QFileInfo fileInfo(filePath);
QListWidgetItem *item = new QListWidgetItem();
ui->listWidget->addItem(item);
mVideoFileList.append(filePath);
setVideoNums(mVideoFileList.size());
}
void MainWindow::clear()
{
stopPlay();
ui->listWidget->clear();
mVideoFileList.clear();
setVideoNums(mVideoFileList.size());
}
void MainWindow::startPlay()
{
playVideo(0);
}
void MainWindow::stopPlay()
{
if (mCurrentItem != nullptr)
{
mCurrentItem->setBackgroundColor(QColor(0, 0, 0, 0));
}
mCurrentItem = nullptr;
mIsNeedPlayNext = false;
mPlayer->stop(true);
}
void MainWindow::playVideo(const int &index)
{
int playIndex = index;
// ///播放到最后一个后,从头开始播放
// {
// if ((playIndex < 0) || (mVideoFileList.size() <= playIndex))
// {
// playIndex = 0;
// }
// }
if (index >= 0 && mVideoFileList.size() > playIndex)
{
mCurrentIndex = playIndex;
QString filePath = mVideoFileList.at(playIndex);
qDebug()<<__FUNCTION__<<filePath<<playIndex;
playVideoFile(filePath);
// ui->listWidget->setCurrentRow(playIndex);
QListWidgetItem *item = ui->listWidget->item(playIndex);
if (mCurrentItem != nullptr)
{
mCurrentItem->setBackgroundColor(QColor(0, 0, 0, 0));
}
mCurrentItem = item;
mCurrentItem->setBackgroundColor(QColor(75, 92, 196));
}
}
void MainWindow::playVideoFile(const QString &filePath)
{
mIsNeedPlayNext = false;
mPlayer->stop(true);
mPlayer->startPlay(filePath.toStdString());
}
void MainWindow::slotSliderMoved(int value)
{
if (QObject::sender() == ui->horizontalSlider)
{
mPlayer->seek((qint64)value * 1000000);
}
else if (QObject::sender() == ui->horizontalSlider_2)
{
mPlayer->setVolume(value / 100.0);
ui->label_volume->setText(QString("%1").arg(value));
}
}
void MainWindow::slotTimerTimeOut()
{
if (QObject::sender() == mTimer)
{
qint64 Sec = mPlayer->getCurrentTime();
ui->horizontalSlider->setValue(Sec);
QString curTime;
QString sStr = QString("0%1").arg(Sec % 60);
if (hStr == "00")
{
curTime = QString("%1:%2").arg(mStr.right(2)).arg(sStr.right(2));
}
else
{
curTime = QString("%1:%2:%3").arg(hStr).arg(mStr.right(2)).arg(sStr.right(2));
}
ui->label_currenttime->setText(curTime);
}
else if (QObject::sender() == mTimer_CheckControlWidget)
{
mTimer_CheckControlWidget->stop();
hideControlWidget();
}
}
void MainWindow::doAdd()
{
QStringList fileList = QFileDialog::getOpenFileNames(
this, QStringLiteral("选择要播放的文件"),
AppConfig::gVideoFilePath,//初始目录
QStringLiteral("视频文件 (*.flv *.rmvb *.avi *.MP4 *.mkv);;")
+QStringLiteral("音频文件 (*.mp3 *.wma *.wav);;")
+QStringLiteral("所有文件 (*.*)"));
运行结果
三、在线协助:
如需安装运行环境或远程调试,可点击博主头像,进入个人主页查看博主联系方式,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作
博主个人主页:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主所有文章点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主联系方式点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc