音视频播放器

慕容安易
2023-12-01

头文件.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSplitter>
#include <QListWidget>
#include <QVideoWidget>
#include <QSlider>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QFileInfoList>
#include <QFileDialog>
#include <QStringList>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QUrl>
#include <QMessageBox>
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void init_ui();
public:
    QListWidget *lw;
    QVideoWidget *vw;
    QLabel *lb_volum;
    QLabel *lb_process;
    QLabel *lb_volum_count;
    QLabel *lb_process_count;
    QSlider *sld_process;
    QSlider *sld_volum;

    QSplitter *spl;
    QWidget *w1;

    QPushButton *bnt_play;
    QPushButton *bnt_stop;
    QPushButton *bnt_pause;
    QPushButton *bnt_add_src;
    QPushButton *bnt_pre_play;
    QPushButton *bnt_next_play;
    QPushButton *bnt_quick_next;
    QPushButton *bnt_quick_pre;
    QPushButton *bnt_two_speed;

    QHBoxLayout *hb1;
    QHBoxLayout *hb2;
    QHBoxLayout *hb3;
    QHBoxLayout *hb4;

    QVBoxLayout *vb1;
    QGridLayout *gb1;
    QStringList mylist;

    QMediaPlayer *myplayer;
    QAudioOutput *audio_output;

    QString cur_all_time;

    int play_stat;

    int paly_stast;
    int speed_stast;
public slots:
    void add_src();
    void play_cur_item();
    void change_cur_all_time();
    void update_time();
    void change_volum();
    void play_src();
    void stop_play();
    void pause_play();
    void change_positation();
    void play_pre_src();
    void play_next_src();
    void quick_pre();
    void quick_next();
    void set_two_speed();

};
#endif // WIDGET_H

.cpp文件

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->init_ui();
    this->mylist.clear();
    this->myplayer = new QMediaPlayer();
    this->audio_output = new QAudioOutput();
    this->myplayer->setAudioOutput(this->audio_output);
    this->myplayer->setVideoOutput(this->vw);
    this->play_stat = 0;
    this->audio_output->setVolume(60);
    this->sld_volum->setValue(60);

    this->play_stat = 0;
    connect(this->bnt_add_src,&QPushButton::clicked,this,&Widget::add_src);
    connect(this->lw,&QListWidget::doubleClicked,this,&Widget::play_cur_item);
    connect(this->myplayer,&QMediaPlayer::durationChanged,this,&Widget::change_cur_all_time);
    connect(this->myplayer,&QMediaPlayer::positionChanged,this,&Widget::update_time);
    connect(this->sld_volum,&QSlider::valueChanged,this,&Widget::change_volum);
    connect(this->bnt_play,&QPushButton::clicked,this,&Widget::play_src);
    connect(this->bnt_stop,&QPushButton::clicked,this,&Widget::stop_play);
    connect(this->bnt_pause,&QPushButton::clicked,this,&Widget::pause_play);
    connect(this->sld_process,&QSlider::sliderPressed,this->myplayer,&QMediaPlayer::pause);
    connect(this->sld_process,&QSlider::sliderPressed,this,&Widget::change_positation);
    connect(this->bnt_pre_play,&QPushButton::clicked,this,&Widget::play_pre_src);
    connect(this->bnt_next_play,&QPushButton::clicked,this,&Widget::play_next_src);

    connect(this->bnt_quick_pre,&QPushButton::clicked,this,&Widget::quick_pre);
    connect(this->bnt_quick_next,&QPushButton::clicked,this,&Widget::quick_next);
    connect(this->bnt_two_speed,&QPushButton::clicked,this,&Widget::set_two_speed);

}

Widget::~Widget()
{
}

void Widget::init_ui()
{
    qDebug()<<"=========";
    this->lw = new QListWidget();
    this->vw = new QVideoWidget();
    this->vw->setFixedSize(QSize(800,600));
    this->lb_process = new QLabel(tr("进度"));
    this->lb_volum = new QLabel(tr("音量"));
    this->lb_process_count = new QLabel("00:00:00/:00:00");
    this->lb_volum_count = new QLabel("60/100");
    this->bnt_play = new QPushButton(tr("播放"));
    this->bnt_stop = new QPushButton(tr("停止"));
    this->bnt_pause = new QPushButton(tr("暂停"));
    this->bnt_add_src = new QPushButton(tr("加载资源"));
    this->bnt_pre_play = new QPushButton(tr("上一曲"));
    this->bnt_next_play = new QPushButton(tr("下一曲"));
    this->bnt_quick_next = new QPushButton(tr("快进"));
    this->bnt_quick_pre = new QPushButton(tr("快退"));
    this->bnt_two_speed = new QPushButton(tr("二倍速"));

    this->sld_process = new QSlider(Qt::Horizontal);
    this->sld_volum = new QSlider(Qt::Horizontal);

    this->sld_volum->setRange(0,100);
    this->hb1 = new QHBoxLayout();
    this->hb2 = new QHBoxLayout();
    this->hb3 = new QHBoxLayout();
    this->hb4 = new QHBoxLayout();

    this->vb1 = new QVBoxLayout();
    this->gb1 = new QGridLayout();

    this->w1 = new QWidget();
    this->spl = new QSplitter();

    this->hb2->addWidget(this->vw);

    this->hb3->addWidget(this->lb_process);
    this->hb3->addWidget(this->sld_process);
    this->hb3->addWidget(this->lb_process_count);

    this->hb4->addWidget(this->lb_volum);
    this->hb4->addWidget(this->sld_volum);
    this->hb4->addWidget(this->lb_volum_count);

    this->gb1->addWidget(this->bnt_play,0,0,1,1);
    this->gb1->addWidget(this->bnt_stop,0,1,1,1);
    this->gb1->addWidget(this->bnt_pause,0,2,1,1);
    this->gb1->addWidget(this->bnt_add_src,0,3,1,2);
    this->gb1->addWidget(this->bnt_next_play,1,0,1,1);
    this->gb1->addWidget(this->bnt_pre_play,1,1,1,1);
    this->gb1->addWidget(this->bnt_quick_next,1,2,1,1);
    this->gb1->addWidget(this->bnt_quick_pre,1,3,1,1);
    this->gb1->addWidget(this->bnt_two_speed,1,4,1,1);

    this->vb1->addLayout(this->hb2);
    this->vb1->addLayout(this->hb3);
    this->vb1->addLayout(this->hb4);
    this->vb1->addLayout(this->gb1);

    this->w1->setLayout(this->vb1);

    this->spl->addWidget(this->lw);
    this->spl->addWidget(this->w1);

    this->hb1->addWidget(this->spl);
    this->setLayout(this->hb1);
}
void Widget::add_src()
{
    QStringList temp = QFileDialog::getOpenFileNames(this,tr("请选择多媒体素材"),tr("1") , tr("*.mp3 *.mp4"));
    this->mylist.append(temp);
    this->lw->clear();
    this->lw->addItems(this->mylist);
}void Widget::play_cur_item()
{
    QString filename = this->lw->currentItem()->text();
    this->myplayer->setSource(QUrl(filename));
    this->myplayer->play();
}
void Widget::change_cur_all_time()
{
    this->cur_all_time.clear();
    qint64 d = this->myplayer->duration();
    this->sld_process->setRange(0,d);
    int as = d / 1000;
    int h = as / 3600;
    int m = (as % 3600) / 60;
    int s = (as % 3600) % 60;
    QString tim = "00:00:00/";
    this->cur_all_time += QString::number(h);
    this->cur_all_time += ":";
    this->cur_all_time += QString::number(m);
    this->cur_all_time += ":";
    this->cur_all_time += QString::number(s);
    this->lb_process_count->setText(tim+this->cur_all_time);
}
void Widget::update_time()
{
    qint64 d = this->myplayer->position();
    this->sld_process->setValue(d);
    int as = d / 1000;
    int h = as / 3600;
    int m = (as % 3600) / 60;
    int s = (as % 3600) % 60;

    QString cur_time;
    cur_time += QString::number(h);
    cur_time += ":";
    cur_time += QString::number(m);
    cur_time += ":";
    cur_time += QString::number(s);
    this->lb_process_count->setText(cur_time+"/"+this->cur_all_time);
}

void Widget::change_volum()
{
    this->audio_output->setVolume(this->sld_volum->value());
    this->lb_volum_count->setText(QString::number(this->sld_volum->value())+"/100");
}
void Widget::play_src()
{
    if(this->lw->count() > 0)
    {
        if(this->lw->currentRow() < 0)
        {
            QMessageBox::warning(this,tr("提示信息:"),tr("没有选择播放资源!"));
            return ;
        }
        QString fn = this->lw->currentItem()->text();
        QFileInfo *fi = new QFileInfo(fn);
        if(fi->isDir())
        {
            QMessageBox::warning(this,tr("提示信息:"),tr("没有选择正确的播放资源!"));
            return ;
        }
        else if(fi->fileName().endsWith(".mp3")||fi->fileName().endsWith(".mp4"))
        {
            if(this->play_stat == 0)
            {
                this->play_cur_item();
            }
            else
            {
                this->myplayer->play();
                this->play_stat = 0;
            }
        }
        else
        {
            QMessageBox::warning(this,tr("提示信息:"),tr("没有选择正确的播放资源!"));
            return ;
        }
    }
    else
    {
        QMessageBox::warning(this,tr("提示信息:"),tr("播放资源为空!"));

    }
}

void Widget::stop_play()
{
    this->myplayer->stop();
}
void Widget::pause_play()
{
    this->myplayer->pause();
    this->play_stat = 1;
}

void Widget::change_positation()
{
    this->myplayer->setPosition(this->sld_process->value());
    this->myplayer->play();
}
void Widget::play_pre_src()
{
    int index = this->lw->currentRow();
    if(index <= 0)
    {
        QMessageBox::warning(this,tr("提示信息:"),tr("已经是第一个资源!"));
        return;
    }
    else if(index > this->lw->count()-1)
    {
        QMessageBox::warning(this,tr("提示信息:"),tr("没有选中资源!"));
        return;
    }
    else
    {
        this->lw->setCurrentRow(index-1);
        this->play_src();
    }
}
void Widget::play_next_src()
{
    int index = this->lw->currentRow();
    if(index >= this->lw->count()-1)
    {
        QMessageBox::warning(this,tr("提示信息:"),tr("已经是最后一个资源!"));
        return;
    }
    else if(index < 0)
    {
        QMessageBox::warning(this,tr("提示信息:"),tr("没有选中资源!"));
        return;
    }
    else
    {
        this->lw->setCurrentRow(index+1);
        this->play_src();
    }
}
void Widget::quick_pre()
{
    //this->myplayer->pause();
    int curpos = this->myplayer->position();
    this->myplayer->setPosition(curpos-5000);
    //this->myplayer->play();
}
void Widget::quick_next()
{
    //this->myplayer->pause();
    int curpos = this->myplayer->position();
    this->myplayer->setPosition(curpos+5000);
    //this->myplayer->play();
}
void Widget::set_two_speed()
{
    if(this->speed_stast == 0)
    {
        this->myplayer->setPlaybackRate(2.0);
        this->speed_stast = 1;
    }
    else
    {
        this->myplayer->setPlaybackRate(1.0);
        this->speed_stast = 0;
    }
}


 类似资料: