Simple audio and video player based on QT

Table of contents:

        1. Interface layout

        2. Play local audio

                2.1 Open the local audio save path

                2.2 Select the audio you want to play and add it to the playlist

                2.3 Play low-latency audio effects

                2.4 Realize related playback operations

        3. Play local video

                3.1 Open the local video save path

                3.2 Select the video you want to play and add it to the playlist

               3.3 Realize relevant playback operations

                3.4 Use QMovie to play local video

1. Interface layout

1. The interface layout here is the way to drag the control layout on the UI interface. The interface background and control style can be set according to your own preferences; the icon of the control needs to download the .png style icon by yourself and add it to the resource folder.

 2. Function realization

        The following code needs to be added in the 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);
}

2. Play local audio

       1. Open the local folder of the audio, and add the audio to be played to the playlist

3. Play local video 

        1. Open the local folder of the video, and add the video to be played into the playlist

        2. Play video

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_72714790/article/details/127218621