实现效果
代码
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QVideoWidget>
#include <QMediaPlayer>
#include <QPushButton>
#include <QSlider>
#include <QComboBox>
#include <QListWidget>
#include <QLabel>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void openFile();
void playSong();
void doubleClickPlay(QListWidgetItem *item);
void setTimeSlider(qint64 duration);
void updateTimeSlider(qint64 position);
void updateTime_NameShow();
void updateSingPos(int positoin);
void updateVolume(int positoin);
void pre();
void next();
private:
Ui::Widget *ui;
QMediaPlayer *mediaPlayer;
QVBoxLayout *vbox;
QHBoxLayout *hbox1;
QHBoxLayout *hbox2;
QHBoxLayout *hbox3;
QPushButton *openBtn;
QPushButton *upBtn;
QPushButton *downBtn;
QPushButton *playBtn;
QComboBox *comboBox;
QSlider *volumeSlider;
QSlider *timeSlider;
QListWidget *listWidget;
QLabel *label;
QLabel *labName;
int n;
QString singName;
QTimer *timer;
QStringList nameList;
int index;
};
#endif
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->vbox = new QVBoxLayout(this);
this->setLayout(this->vbox);
this->hbox1 = new QHBoxLayout(this);
this->hbox2 = new QHBoxLayout(this);
this->hbox3 = new QHBoxLayout(this);
this->mediaPlayer = new QMediaPlayer(this);
this->listWidget = new QListWidget(this);
this->label = new QLabel("00:00/00:00",this);
this->labName = new QLabel("暂无音乐",this);
this->volumeSlider = new QSlider(this);
this->volumeSlider->setMaximumWidth(200);
this->timeSlider = new QSlider(this);
this->comboBox = new QComboBox(this);
this->comboBox->addItem("顺序播放");
this->comboBox->addItem("随机播放");
this->comboBox->addItem("循环播放");
this->volumeSlider->setOrientation(Qt::Horizontal);
this->timeSlider->setOrientation(Qt::Horizontal);
this->volumeSlider->setMinimumWidth(100);
this->volumeSlider->setRange(0,100);
this->volumeSlider->setValue(50);
this->openBtn = new QPushButton("打开",this);
this->upBtn = new QPushButton("上一首",this);
this->downBtn = new QPushButton("下一首",this);
this->playBtn = new QPushButton("播放",this);
this->playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
this->timer = new QTimer(this);
this->n = 0;
this->index = 0;
hbox1->addWidget(this->openBtn);
hbox1->addWidget(this->upBtn);
hbox1->addWidget(this->downBtn);
hbox1->addWidget(this->playBtn);
hbox1->addWidget(this->comboBox);
hbox2->addWidget(this->label);
hbox2->addWidget(this->labName);
hbox3->addWidget(this->timeSlider);
hbox3->addWidget(this->volumeSlider);
vbox->addWidget(this->listWidget);
vbox->addLayout(this->hbox2);
vbox->addLayout(this->hbox3);
vbox->addLayout(this->hbox1);
connect(this->openBtn,SIGNAL(clicked()),this,SLOT(openFile()));
connect(this->playBtn,SIGNAL(clicked()),this,SLOT(playSong()));
connect(this->mediaPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(updateTimeSlider(qint64)));
connect(this->mediaPlayer,SIGNAL(durationChanged(qint64)),this,SLOT(setTimeSlider(qint64)));
connect(this->timer,SIGNAL(timeout()),this,SLOT(updateTime_NameShow()));
connect(this->timeSlider,SIGNAL(sliderMoved(int)),this,SLOT(updateSingPos(int)));
connect(this->volumeSlider,SIGNAL(sliderMoved(int)),this,SLOT(updateVolume(int)));
connect(this->listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(doubleClickPlay(QListWidgetItem*)));
connect(this->upBtn,SIGNAL(clicked()),this,SLOT(pre()));
connect(this->downBtn,SIGNAL(clicked()),this,SLOT(next()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::openFile()
{
this->nameList.clear();
this->listWidget->clear();
this->nameList = QFileDialog::getOpenFileNames(this,"select",".","MP3(*.mp3)");
for(int i = 0;i < nameList.size();i++)
{
QFileInfo info(nameList.at(i));
QListWidgetItem *item = new QListWidgetItem(info.fileName(),this->listWidget);
this->listWidget->addItem(item);
item->setToolTip(nameList.at(i));
}
QString fileName = nameList.at(0);
this->mediaPlayer->setMedia(QUrl(fileName));
QFileInfo info(fileName);
this->singName = info.fileName();
this->labName->setText(this->singName);
this->mediaPlayer->setVolume(50);
this->mediaPlayer->play();
this->timer->start(1000);
this->playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPause));
}
void Widget::playSong()
{
int state = this->mediaPlayer->state();
if(state == QMediaPlayer::PlayingState)
{
this->mediaPlayer->pause();
this->playBtn->setText("播放");
this->playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
}
else if (state == QMediaPlayer::PausedState) {
this->mediaPlayer->play();
this->playBtn->setText("暂停");
this->playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPause));
}
}
void Widget::doubleClickPlay(QListWidgetItem *item)
{
this->index = this->listWidget->currentRow();
this->mediaPlayer->setMedia(QUrl(item->toolTip()));
this->labName->setText(item->text());
this->mediaPlayer->play();
this->timer->start(1000);
this->playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPause));
}
void Widget::setTimeSlider(qint64 duration)
{
this->timeSlider->setRange(0,int(duration));
}
void Widget::updateTimeSlider(qint64 position)
{
this->timeSlider->setValue(int(position));
}
void Widget::updateTime_NameShow()
{
this->n++;
if(this->n == this->singName.size())
{
this->n = 0;
}
this->labName->setText(this->singName.mid(this->n));
qint64 dur = this->mediaPlayer->duration();
qint64 pos = this->mediaPlayer->position();
QTime durTime(0,0,0);
QTime posTime(0,0,0);
this->label->setText(posTime.addMSecs(int(pos)).toString("mm:ss") + "/" + durTime.addMSecs(int(dur)).toString("mm:ss"));
if(dur == pos)
{
if(this->comboBox->currentText() == "顺序播放")
{
this->index++;
if(this->index > this->nameList.size() - 1)
{
this->index = 0;
}
}
else if (this->comboBox->currentText() == "随机播放") {
this->index = qrand() % this->nameList.size();
}
else {
this->index = this->index;
}
QFileInfo fileName(this->nameList.at(this->index));
this->mediaPlayer->setMedia(QUrl(this->nameList.at(this->index)));
this->labName->setText(fileName.fileName());
this->mediaPlayer->play();
this->timer->start(1000);
}
}
void Widget::updateSingPos(int positoin)
{
this->mediaPlayer->setPosition(positoin);
}
void Widget::updateVolume(int positoin)
{
this->mediaPlayer->setVolume(positoin);
}
void Widget::pre()
{
this->index--;
if(this->index < 0)
{
this->index = this->nameList.size() - 1;
}
this->mediaPlayer->stop();
QFileInfo fileName(this->nameList.at(this->index));
this->mediaPlayer->setMedia(QUrl(this->nameList.at(this->index)));
this->labName->setText(fileName.fileName());
this->mediaPlayer->play();
this->timer->start(1000);
}
void Widget::next()
{
this->index++;
if(this->index > this->nameList.size() - 1)
{
this->index = 0;
}
this->mediaPlayer->stop();
QFileInfo fileName(this->nameList.at(this->index));
this->mediaPlayer->setMedia(QUrl(this->nameList.at(this->index)));
this->labName->setText(fileName.fileName());
this->mediaPlayer->play();
this->timer->start(1000);
}