基于QT实现简易音视频播放器

目录:

        一、界面布局

        二、播放本地音频

                2.1 打开本地音频保存路径

                2.2 选中想要播放的音频加入到播放列表

                2.3 播放低延迟音频效果

                2.4 实现相关播放操作

        三、播放本地视频

                3.1 打开本地视频保存路径

                3.2 选中想要播放的视频加入到播放列表

                3.3 实现相关播放操作

                3.4 使用QMovie播放本地视频

一、界面布局

1、这里的界面布局是UI界面拖动控件布局的方式,界面背景和控件样式可以根据自己的喜好自行设置;控件的图标需要自己下载.png样式的图标,并且添加到资源文件夹下。

 2、功能实现

        pro里面的需要添加如下代码

QT       += core gui multimedia multimediawidgets

       · widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include <QFileDialog>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
protected:
    void paintEvent(QPaintEvent *event);

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_pushButton_open_clicked();                          //打开
    void on_pushButton_pre_clicked();                           //上一个
    void on_pushButton_start_stop_clicked();                    //暂停——播放
    void on_pushButton_next_clicked();                          //下一个
    void on_pushButton_stop_clicked();                          //结束
    void on_horizontalSlider_pace_valueChanged(int value);      //进度
    void on_verticalSlider_volume_valueChanged(int value);      //音量

private:
    Ui::Widget *ui;
    QMediaPlayer *myplayer = nullptr;           //播放者
    QMediaPlaylist *myplayerlist = nullptr;     //播放列表
    QVideoWidget *mywidget = nullptr;           //在哪播放

    bool isstart = false;                       //初始播放状态为false
};

#endif // WIDGET_H

        ·widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //播放者
    myplayer = new QMediaPlayer;
    //播放列表
    myplayerlist = new QMediaPlaylist;
    //在哪播放
    mywidget = new QVideoWidget(ui->label);

    myplayer->setPlaylist(myplayerlist);
    myplayer->setVideoOutput(mywidget);
    mywidget->resize(ui->label->size());    //设置播放界面大小
    setWindowTitle("QT-视频播放器");          //设置界面名称

    myplayer->setVolume(50);                     //设置初始音量为50
    ui->verticalSlider_volume->setRange(0,100);//设置音量范围
    ui->verticalSlider_volume->setValue(50);   //设置滑动杆的初始位置为50
    //设置播放进度
    connect(myplayer,&QMediaPlayer::positionChanged,ui->horizontalSlider_pace,&QSlider::setValue);
    connect(myplayer,&QMediaPlayer::durationChanged,ui->horizontalSlider_pace,[&](qint64 temp){
                ui->horizontalSlider_pace->setRange(0,(int)temp);
            });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    mywidget->resize(ui->label->size());//设置播放界面大小为标签大小
}
//打开
void Widget::on_pushButton_open_clicked()
{
    //打开文件
    QStringList mylist = QFileDialog::getOpenFileNames(this,"选择播放路径",                         //弹出"选择播放路径"窗口
                                                       "D:\\Qt\\220701QT\\VideoPlayer\\音视频",    //播放路径
                                                       "allfiles(*.*);;"                          //所以文件类型
                                                       "MP3(*.mp3);;"                             //音频
                                                       "MP4(*.mp4);;");                           //视频
    myplayerlist->clear();
    for(const auto &k:mylist)
        myplayerlist->addMedia(QUrl(k));
}
//上一个
void Widget::on_pushButton_pre_clicked()
{
    myplayer->stop();
    myplayerlist->previous();
    myplayer->play();
}
//暂停——播放
void Widget::on_pushButton_start_stop_clicked()
{
    if(isstart)
    {
        ui->pushButton_start_stop->setStyleSheet("QPushButton#pushButton_start_stop{border-image:url(:/播放.png)}");
        isstart = false;
        myplayer->pause();//暂停
    }
    else
    {
        ui->pushButton_start_stop->setStyleSheet("QPushButton#pushButton_start_stop{border-image:url(:/暂停.png)}");
        isstart = true;
        myplayer->play();
        ui->pushButton_stop->setEnabled(true);
    }
}
//下一个
void Widget::on_pushButton_next_clicked()
{
    myplayer->stop();
    myplayerlist->next();
    myplayer->play();
}
//结束
void Widget::on_pushButton_stop_clicked()
{
    myplayer->stop();
    ui->pushButton_stop->setEnabled(false);
    ui->pushButton_start_stop->setStyleSheet("QPushButton#pushButton_start_stop{border-image:url(:/播放.png)}");
}
//进度
void Widget::on_horizontalSlider_pace_valueChanged(int value)
{
    myplayer->setPosition((qint64)value);
}
//音量
void Widget::on_verticalSlider_volume_valueChanged(int value)
{
    myplayer->setVolume(value);
}

二、播放本地音频

        1、打开音频的本地文件夹,将需要播放的音频加入的播放列表中

三、播放本地视频 

        1、打开视频的本地文件夹,将需要播放的视频加入的播放列表中

        2、播放视频

 

 

 

 

  • 3
    点赞
  • 19
    收藏
  • 打赏
    打赏
  • 3
    评论
<ul class="ul1"> <li class="li1" style="margin: 0px 0px 42px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 38px; line-height: normal; font-family: 'PingFang SC Light'; color: #ffffff; text-align: left;"> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">课程摘要</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">1 Qt音视频开发实战 是陈超老师用10年以上音视频一线实战开发经验,四年音视频开发教学经验打造的Qt音视频开发课程。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">2 Qt音视频开发实战以Qt c++开发为中心。所有课程全部围绕Qt音视频开发展开。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">3 非常适合那些只做Qt开发的工程师来学习,学习更容易一些。学习目标,培养高级Qt音视频开发工程师.</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">4 Qt音视频开发实战适合那些掌握Qt基础开发,初级开发工程师,月薪在20k以下,通过学习,达到更高薪水的Qt高级开发工程师。</h1> <h1 class="p2" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; min-height: 14px; color: #000000;"> </h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">课程优势</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">1 老师过去15年+ 音视频,网络通讯,数字图像处理一线实战经验。过去四年积累了丰富的音视频开发教学经验,学员从年薪30w~50w,就职于腾讯,阿里等各大互联网公司。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">2 老师10多年Qt开发经验,长期大量使用Qt开发项目,国内最早一批使用Qt的程序员。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">3 QQ 一对一教学指导,阶段性作业案例小项目指导,就业面试指导。</h1> <h1 class="p2" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; min-height: 14px; color: #000000;"> </h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">课程内容</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">1 Qt 核心基础加强。对于那些学了很多年Qt开发而不得要领的学员非常友好。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">2 音视频开发基础。音视频原理,RGB YUV, 频原理。图像压缩编码,频压缩编码,H.264压缩,H.264编码原理 I P B SPS PPS解析,</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;"><span class="Apple-converted-space">   </span>视频存储容器,mp4 , AAC.<span class="Apple-converted-space">  </span>Qt频视频采集,FFmpeg编码,解码,x264编码,AAC编码。mp3编码。格式转换。视频播放器内核。图像渲染。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">3 OpenGL数字图像处理基础。OpenGL基础,渲染管线,shader编程。滤镜,美颜,后期处理。编写渲染引擎。</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">4 网络socket通讯编程,自定义私有协议。TCP/UDP音视频传输。</h1> <h1 class="p2" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; min-height: 14px; color: #000000;"> </h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">学员要求</h1> <h1 class="p1" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #000000;">1 熟悉c/c++,掌握Qt基本控件,类基础。</h1> <h1 class="p3" style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'PingFang SC'; color: #000000;"><span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: 'Helvetica Neue';">2 </span>每天学习两小时,学习周期<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: 'Helvetica Neue';">3</span>~<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: 'Helvetica Neue';">5</span>个月,做完练习,小项目。</h1> </li> </ul>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫沫.♡

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值