C++ QT视频音乐播放器

简介: C++ QT视频音乐播放器
程序示例精选
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
相关文章
|
3月前
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
91 1
|
3月前
|
Web App开发 编解码 安全
视频会议技术 入门探究:WebRTC、Qt与FFmpeg在视频编解码中的应用
视频会议技术 入门探究:WebRTC、Qt与FFmpeg在视频编解码中的应用
433 4
|
3月前
|
设计模式 缓存 编译器
【C++ 元对象系统03】深入探索Qt反射:从原理到实践
【C++ 元对象系统03】深入探索Qt反射:从原理到实践
181 4
|
3月前
|
安全 网络协议 数据安全/隐私保护
掌握Qt和C++:构建你的第一个P2P应用程序
掌握Qt和C++:构建你的第一个P2P应用程序
206 3
|
3月前
|
C++
基于Qt的简易音乐播放器设计与实现
基于Qt的简易音乐播放器设计与实现
172 0
|
3月前
|
存储 算法 API
【Qt 基本类】QDateTime类在C++中的应用与深度解析
【Qt 基本类】QDateTime类在C++中的应用与深度解析
124 0
|
3月前
|
存储 网络协议 C语言
【C/C++ 串口编程 】深入探讨C/C++与Qt串口编程中的粘包现象及其解决策略
【C/C++ 串口编程 】深入探讨C/C++与Qt串口编程中的粘包现象及其解决策略
269 0
|
3月前
|
存储 传感器 安全
【串口通信】使用C++和Qt设计和实现串口协议解析器(二)
【串口通信】使用C++和Qt设计和实现串口协议解析器
281 0
|
1月前
|
数据安全/隐私保护 C++ 计算机视觉
Qt(C++)开发一款图片防盗用水印制作小工具
文本水印是一种常用的防盗用手段,可以将文本信息嵌入到图片、视频等文件中,用于识别和证明文件的版权归属。在数字化和网络化的时代,大量的原创作品容易被不法分子盗用或侵犯版权,因此加入文本水印成为了保护原创作品和维护知识产权的必要手段。 通常情况下,文本水印可以包含版权声明、制作者姓名、日期、网址等信息,以帮助识别文件的来源和版权归属。同时,为了增强防盗用效果,文本水印通常会采用字体、颜色、角度等多种组合方式,使得水印难以被删除或篡改,有效地降低了盗用意愿和风险。 开发人员可以使用图像处理技术和编程语言实现文本水印的功能,例如使用Qt的QPainter类进行文本绘制操作,将文本信息嵌入到图片中,
99 1
Qt(C++)开发一款图片防盗用水印制作小工具
|
10天前
|
存储 C++
【C++】C++ 基于QT实现散列表学生管理系统(源码+数据+课程论文)【独一无二】
【C++】C++ 基于QT实现散列表学生管理系统(源码+数据+课程论文)【独一无二】
【C++】C++ 基于QT实现散列表学生管理系统(源码+数据+课程论文)【独一无二】

热门文章

最新文章

推荐镜像

更多