QT实现MP4播放器

news2024/9/20 22:24:42

1、实现功能

 播放本地MP4文件。

1、可以将文件添加到列表中,双击列表中的歌曲进行播放。

2、实现视频的播放与暂停。

3、上一首和下一首的按钮选择功能。

4、视频名称的滚动显示。

5、当前的播放时长和总时长。

6、播放模式的选择:单曲循环、随机播放、顺序播放。

7、使用了水平和垂直布局。

8、视频音量的控制。

9、使用非UI界面实现。

2、项目环境

Qt Creator 5.0.2 

3、widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QListWidget>
#include <QVideoWidget>
#include <QMediaPlayer>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QMessageBox>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QTime>
#include <QTimer>
#include <QResizeEvent>
#include <QSizePolicy>
#include <QListWidgetItem>
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void get_duration(qint64 duration);
    void update_playslidexvalue(qint64 position);
    void basic();

private slots:
    void on_select_btn_clicked();
    void on_pre_btn_clicked();
    void on_next_btn_clicked();
    void onComboBoxIndexChanged(int index);
    void on_itemDouble_clicked(QListWidgetItem *item);
    void on_play_btn_clicked();
    void on_play_slidex_sliderMoved(int position);
private:
   QListWidget *list;
   QVideoWidget *video;
   QLabel *name_lab;
   QLabel *time_lab;
   QSlider *slider1;
   QSlider *slider2;
   QPushButton *select_btn;
   QPushButton *play_btn;
   QPushButton *pre_btn;
   QPushButton *next_btn;
   QComboBox *combox;

    QHBoxLayout *hbox1;
    QHBoxLayout *hbox2;
    QHBoxLayout *hbox3;

    QVBoxLayout *vbox;

    QMediaPlayer *player;
    QTimer *timer;
    QString song_name;
    int index;
    int lon;
    int hua;
    int mode;
};
#endif // WIDGET_H

 4、widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
        this->setGeometry(0, 0, 800, 600);
        /*------------------------------------*/
        this->list =new QListWidget(this);//设置视频展示列表
       /*------------------------------------*/
        this->video =new QVideoWidget(this);//设置视频播放
        this->video->setWindowTitle("视频播放器");
        this->video->setStyleSheet("border:2px solid black");



         /*------------------------------------*/
        this->name_lab =new QLabel(this);//设置视频名滚动
        this->name_lab->setGeometry(10,380,200,40);
        this->name_lab->setText("视频名");
        /*------------------------------------*/
       this->time_lab =new QLabel(this);//设置播放时间
        this->time_lab->setGeometry(500,380,200,40);
        this->time_lab->setText("     ");



         /*------------------------------------*/
        this->slider1 = new QSlider(this);//播放时间的滑块
        this->slider1->setOrientation(Qt::Horizontal);
        this->slider1->setGeometry(10,440, 780, 30);
         /*------------------------------------*/


        this->select_btn =new QPushButton(this);//添加到列表
        this->select_btn->setGeometry(10,520,100,40);
         this->select_btn->setText("添加到列表");
        /*------------------------------------*/
        this->play_btn =new QPushButton(this);//  播放/暂停
        this->play_btn->setGeometry(130,520,100,40);
         this->play_btn->setText("播放");
        /*------------------------------------*/
        this->pre_btn =new QPushButton(this);//
        this->pre_btn->setGeometry(250,520,100,40);
         this->pre_btn->setText("上一首");
        /*------------------------------------*/
        this->next_btn =new QPushButton(this);//
        this->next_btn->setGeometry(370,520,100,40);
         this->next_btn->setText("下一首");
        /*------------------------------------*/
        this->combox =new QComboBox(this);//
        this->combox->setGeometry(500,520,100,50);
        this->combox->addItem("单曲循环");
        this->combox->addItem("顺序播放");
        this->combox->addItem("随机播放");
         /*------------------------------------*/
        this->slider2 = new QSlider(this);//播放声音的滑块
        this->slider2->setOrientation(Qt::Horizontal);
        this->slider2->setGeometry(620,520, 130, 40);
        /*------------------------------------*/


        //准备一个水平布局
        //给水平布局添加组件对象
        this->hbox1 = new QHBoxLayout;
        this->hbox1->addWidget(this->list,1);
        this->hbox1->addStretch();
        this->hbox1->addWidget(this->video,3);
         /*------------------------------------*/
        this->hbox2 = new QHBoxLayout;
        this->hbox2->addWidget(this->name_lab);
        this->hbox2->addWidget(this->time_lab);
         /*------------------------------------*/
        this->hbox3 = new QHBoxLayout;
        this->hbox3->addWidget(this->select_btn);
        this->hbox3->addWidget(this->play_btn);
        this->hbox3->addWidget(this->pre_btn);
        this->hbox3->addWidget(this->next_btn);
        this->hbox3->addWidget(this->combox);
        this->hbox3->addWidget(this->slider2);
         /*------------------------------------*/
        //准备一个垂直布局
        this->vbox = new QVBoxLayout;
        //给垂直布局添加组件、添加布局
        this->vbox->addLayout(this->hbox1);
        this->vbox->addLayout(this->hbox2);
        this->vbox->addWidget(this->slider1);
        this->vbox->addLayout(this->hbox3);
         /*------------------------------------*/
        //给界面设置布局为垂直布局
        this->setLayout(this->vbox);
      /*------------------------------------*/
      //实例化播放器对象
       this->player=new QMediaPlayer(this);
      /*------------------------------------*/
      this->timer=new QTimer(this);//实例化定时器对象
     /*------------------------------------*/
       this->index=0;//实例化Index
      this->lon=0;//实例化lon
      this->hua=0;//实例化hua
      this->mode=0;//实例化mode  默认为单曲循环
       this->player->setVolume(0);//声音默认为0
     /*------------------------------------*/
      //槽函数与信号关联
    connect(this->select_btn,&QPushButton::clicked,this,&Widget::on_select_btn_clicked);
    // connect(this->select_btn,&QPushButton::clicked,this,&Widget::on_select_btn_clicked);
   connect(this->list, &QListWidget::itemDoubleClicked, this,&Widget::on_itemDouble_clicked);
   connect(this->play_btn,&QPushButton::clicked,this,&Widget::on_play_btn_clicked);
   connect(this->player,&QMediaPlayer::durationChanged,this,&Widget::get_duration);

    connect(this->player,&QMediaPlayer::positionChanged,this,&Widget::update_playslidexvalue);
                                    //  位置改变
   connect(this->slider1,&QSlider::sliderMoved,this,&Widget::on_play_slidex_sliderMoved);

   connect(this->pre_btn,&QPushButton::clicked,this,&Widget::on_pre_btn_clicked);
   connect(this->next_btn,&QPushButton::clicked,this,&Widget::on_next_btn_clicked);

    connect(this->combox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxIndexChanged(int)));


   connect(this->timer,&QTimer::timeout,this,[=]()
    {
         //获取当前的播放时长以及歌曲的总时长
        qint64 pos =this->player->position();
        qint64 dur =this->player->duration();
        QString str = QTime(0,0,0,0).addMSecs(pos).toString("mm:ss")+
                 "/"+ QTime(0,0,0,0).addMSecs(dur).toString("mm:ss");
               //设置给时间标签去显示
        this->time_lab->setText(str);

        //歌曲名称的滚动显示
        this->index++;
        if(this->index==this->song_name.size())
        {
            this->index=0;
        }
        QString name=this->song_name.mid(this->index);
        this->name_lab->setText(name);

        if(dur==pos)
        {
            if(this->mode==0)
            {
                this->player->stop();
                this->player->play();

            }else if(this->mode==1)
            {
                this->player->pause();
               if(this->hua<this->lon-1)
               {
                    this->hua++;
               }
               else
               {
                    this->hua=0;
               }
               basic();

            }else if(this->mode==2)
            {
                this->hua=rand()%this->lon;
                basic();
            }

        }


    });


connect(this->slider2, &QSlider::sliderMoved, this->player, [=](qint64 position)
{
        //改变音量滑块的位置,调节音量的大小
        this->player->setVolume(position);
});


}

Widget::~Widget()
{

}

void Widget::get_duration(qint64 duration)
{ //获取歌曲时长
    //给滑块设置取值范围
    this->slider1->setRange(0,duration);
}

//根据歌曲的播放位置变化改变滑块的位置
void Widget::update_playslidexvalue(qint64 position)
{
   // 改变滑块的值
   this->slider1->setValue(position);

}

void Widget::basic()
{
    //给播放器设置媒体资源
     QListWidgetItem *item = this->list->item(this->hua);
     QString filePath = item->data(Qt::UserRole).toString();
    this->player->setMedia(QMediaContent(QUrl(filePath)));
    //给播放器设置画面输出
    this->player->setVideoOutput(this->video);
    //给滑块设置取值范围
    this->slider1->setValue(0);
    //获取歌曲名称
    this->song_name="                          "+QFileInfo(filePath).fileName();
    this->timer->start(1000);
    this->player->play();
    this->play_btn->setText("暂停");
}


void Widget::on_select_btn_clicked()
{
    //选择歌曲资源
   QStringList filePaths = QFileDialog::getOpenFileNames(this,"选择","./","MP4(*.mp4)");
   for(int i=0;i<filePaths.size();i++)
   {
       QString filePath=filePaths.at(i);
       QString fileName = QFileInfo(filePaths.at(i)).fileName();
       QListWidgetItem *item = new QListWidgetItem(QIcon("./icon/opn.jfif"), fileName);
       //将文件的完整路径存储在 QListwidgetItem 的用户数据中
        item->setData(Qt::UserRole,filePath);
        item->setData(Qt::UserRole+1,this->lon);
        this->lon++;
        this->list->addItem(item);
   }
}



void Widget::on_itemDouble_clicked(QListWidgetItem *item)
{
    this->player->pause();
    QString filePath = item->data(Qt::UserRole).toString();
    this->hua= item->data(Qt::UserRole + 1).toInt();
    basic();
}


void Widget::on_play_btn_clicked()
{
    if(this->lon!=0)
    {
        if(this->play_btn->text()=="播放")
        {
            //启动定时器
            this->timer->start(1000);
            this->player->play();
            this->play_btn->setText("暂停");
        }
        else if(this->play_btn->text()=="暂停")
        {
            this->player->pause();
            this->timer->stop();
            this->play_btn->setText("播放");
        }
    }
    else
    {
        QMessageBox::information(this, "播放", "请先选择视频");
    }
}

void Widget::on_pre_btn_clicked()
{
     this->player->pause();
    if(this->hua>0)
    {
       this->hua--;
       basic();
    }else
    {
        QMessageBox::information(this, "上一首", "这是第一首了");
    }
}

void Widget::on_next_btn_clicked()
{
    this->player->pause();
   if(this->hua<this->lon-1)
   {
        this->hua++;
        basic();
   }else
   {
       QMessageBox::information(this, "下一首", "这是最后一首了");
   }
}

void Widget::onComboBoxIndexChanged(int index)
{
           this->mode=index;
           switch (index)
           {
           case 0:
               qDebug() << "单曲循环模式被选中";
               break;
           case 1:
               qDebug() << "顺序播放模式被选中";
               break;
           case 2:
               qDebug() << "随机播放模式被选中";
               break;
           default:
               break;
           }
}



//根据滑块的位置变化改变歌曲的播放位置
void Widget::on_play_slidex_sliderMoved(int position)
{
    //改变歌曲的播放位置
    this->player->setPosition(position);
}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2091939.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2025年事业编考试证件照有什么要求和条件?

在事业编考试中&#xff0c;证件照作为考生身份识别的重要材料&#xff0c;其要求十分严格。一般而言&#xff0c;合格的事业编考试证件照需满足以下要求&#xff1a; 图片组合包活动地址&#xff1a;点击进入 一、事业编考试证件照要求 格式与大小&#xff1a;照片应为JPG或…

c++ STL 容器相关

容器库&#xff1a;容器库 - cppreference.com 一、std::priority_queue #include <queue> template<class T,class Container std::vector<T>,class Compare std::less<typename Container::value_type> > class priority_queue; 优先级队列是一…

学习资料销售平台小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;资料类型管理&#xff0c;学习资料管理&#xff0c;订单管理&#xff0c;系统管理 微信端账号功能包括&#xff1a;系统首页&#xff0c;学习资料&#xff0c;购物车&#xff0c;…

C/C++二维码生成库qrencode编译与使用

1 qrencode介绍 qrencode 是一个用于生成 QR 码&#xff08;Quick Response Code&#xff09;的工具和库。QR 码是一种矩阵条形码或二维条形码&#xff0c;能够在水平和垂直方向上存储信息。qrencode 支持多种编码方式&#xff0c;并能够生成不同复杂度和错误校正级别的 QR 码…

计算机组成原理:实验四常规型微程序控制器组成实验

一、实验目的 1.掌握时序产生器的组成原理。 2.掌握微程序控制器的组成原理。 3.掌握微指令格式的化简和归并。 二、实验电路 1.时序发生器 TEC-4计算机组成原理实验系统的时序电路如图4.1所示。电路采用2片GAL22V10&#xff08;U6、U7&#xff09;&#xff0c;可产生两极…

微信小程序App实现小车方向控制

目录 概述 1 系统框架结构 1.1 结构介绍 1.2 硬件模块介绍 1.2.1 蓝牙模块介绍 1.2 .2 模块功能介绍 2 功能实现 2.1 微信小程序APP 2.2 下位机功能 3 功能测试 3.1 小程序UI测试 3.2 小车方向控制 微信小程序和蓝牙模块控制小车运行状态 概述 本文主要介绍使用微信…

vue.js项目实战案例源码

Vue.js是一个用于构建用户界面的渐进式框架&#xff0c;非常适合开发单页面应用。以下是一些实战案例的源码&#xff0c;可以帮助你更好地理解和使用Vue.js&#xff1a; Vue TodoMVC 简介&#xff1a;这是学习Vue.js的入门级项目&#xff0c;主要实现了一个待办事项列表。源码地…

Python优化算法17——黄金正弦算法(GSA)

科研里面优化算法都用的多&#xff0c;尤其是各种动物园里面的智能仿生优化算法&#xff0c;但是目前都是MATLAB的代码多&#xff0c;python几乎没有什么包&#xff0c;这次把优化算法系列的代码都从底层手写开始。 需要看以前的优化算法文章可以参考&#xff1a;Python优化算…

从 CRX 文件安装 Chrome 扩展程序

在使用嵌入式 Browser 中的扩展程序时&#xff0c;您可能希望将它们打包并分发在应用程序中&#xff0c;并静默安装。 在本教程中&#xff0c;我将演示如何通过编程方式从 CRX 文件中安装扩展程序&#xff0c;保持它们的更新&#xff0c;并使用它们。 此外&#xff0c;我还会…

8.30工作笔记

要做的事情&#xff1a; 1 测试剩下的三个因子&#xff1a;coppock 潮汐因子 云开雾散 2 整理需要时间序列的因子 以及截面因子 3 灾后重建多了一列&#xff0c;灾后重建’所有值都是nan&#xff0c;这里不仅是灾后重建&#xff0c;所有的都要改 4 coppock 潮汐因子 云开雾散在…

排列数+时间戳+逆元取模

前言&#xff1a;这个题目是真的难&#xff0c;不会做&#xff0c;看了题解才发现是咋回事 题目地址 最主要的就是为啥是除以3&#xff0c;c之前需要完成a 和 b&#xff0c;d 和 e 对我们的答案没有影响&#xff0c;所以我们要除以 A(3,3) ,但是 a 和 b 的排列没有要求&#xf…

Sinc Function介绍

1、定义 Sinc函数全称&#xff1a;sine cardinal&#xff0c;也称作是sampling function&#xff08;采样函数&#xff09;。 2、分类 &#xff08;1&#xff09;归一化sinc函数&#xff1a; 这种定义在信号处理中被广泛采用&#xff0c;其中 x 是一个无量纲的变量&#xff0c;…

基于YOLO的车牌检测识别(YOLO+Transformer)

概述&#xff1a; 基于深度学习的车牌识别&#xff0c;其中&#xff0c;车辆检测网络直接使用YOLO侦测。而后&#xff0c;才是使用网络侦测车牌与识别车牌号。 车牌的侦测网络&#xff0c;采用的是resnet18&#xff0c;网络输出检测边框的仿射变换矩阵&#xff0c;可检测任意形…

同城小程序怎么做 同城小程序系统开发制作方案

很多同城创业的老板们想要做一个同城小程序但是不知道怎么做&#xff0c;本次瀚林就为大家详细介绍一下做同城小程序系统开发制作方法&#xff0c;给大家做个参考。 目前同城类型的小程序系统市面上比较常见的有&#xff1a;同城配送、鲜花订花、同城上门服务、同城跑腿、同城便…

中仕公考怎么样?事业编考试怎么备考?

事业编考试备考可以大致分为三个阶段&#xff0c;按照不同阶段根据自身的学习情况制定不同的学习计划即可。 ①基础阶段 有备考经验的考生可以忽略这一步&#xff0c;刚开始先打好基础很重要&#xff0c;根据课程和教材理解知识点&#xff0c;按照模块学习&#xff0c;对考试…

cnocr 安装

打开终端 如果不会打开终端 -> 终端打开输入 pip install cnocr 执行中途可能报错 去这里下载工具&#xff1a;c构建工具下载完打开&#xff0c;勾选这个 然后点安装安装完回到第2步重新执行

docker镜像所使用到的COW写时复制技术是什么

copy on write 简单来说&#xff0c;所有的读操作都是指向一份内存地址&#xff0c;共享这些数据&#xff0c;节省内存空间。 如果有进程要对数据进行写操作&#xff0c;系统会检测到这个行为&#xff0c;将数据复制一份出来&#xff0c;给这个进程进行写操作。其他进程继续…

5.3二叉树——二叉树链式结构实现

本篇博客梳理二叉树链式结构 明确&#xff1a;二叉树是递归定义的 递归的本质&#xff1a;当前问题子问题&#xff0c;返回条件是最小规模的子问题 一、二叉树的遍历 1&#xff0e;前序、中序与后序遍历 &#xff08;1&#xff09;前序&#xff1a;根->左子树->右子树…

全球知名度最高的华人颜廷利:世界公认十大思想家哲学家

全球知名度最高的华人颜廷利&#xff1a;世界公认十大思想家哲学家 在汉语这一中国优秀传统文化的瑰宝中&#xff0c;“色”与“舍”这两个字的发音分别被解读为“思恶”和“识恶”&#xff0c;揭示了一种深奥的文化现象。这种现象的根源&#xff0c;实则来自于我们的感官——眼…

linux上查找某应用所在的绝对路径

linux上查找某应用所在的绝对路径 1、已知应用名称 找到应用的进程号 例&#xff1a;查找nginx的进程号 ps -ef | grep nginx 或者 ps -aux | grep nginx 2、通过端口号找进程号 lsof -i:80 3、通过进程号找到所在目录&#xff0c;Linux在启动一个进程时,系统会在/proc目…