Qt实战(三)——音乐播放器

音乐播放器逻辑

  首先通过打开文件,获取文件夹下mp3文件的名字,将其存入QTableWidget控件下,通过双击QTableWidget空间中的内容,播放相应的mp3音乐文件,同时通过使用QMediaPlaylist来实现上一首,下一首的功能。

效果图

Qt实战(三)——音乐播放器_第1张图片

具体实现

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
class QMediaPlayer;
class QMediaPlaylist;

class QLabel;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    void InitWidget();
    ~Widget();

private slots:
    void on_pushButton_clicked();
    void on_pushButton_5_clicked();
    void musicplayer(int,int);
    void on_pushButton_7_clicked();
    void on_pushButton_8_clicked();
    void on_pushButton_6_clicked(bool checked);
    void valueChanged(qint64 value);
    void setPositon(int);
    void updateDuration(qint64 duration);

protected:

private:
    void findFile(const QString&path,std::vector<QString>& fileNames);
    Ui::Widget *ui;
    QString path;
    QMediaPlayer *player;
    QMediaPlaylist *playlist;
    int num_music;
    int status = 1;
    qint64 position;
};

#endif // WIDGET_H

widget.cpp

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

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    player = new QMediaPlayer(this);
    playlist = new QMediaPlaylist;
    ui->setupUi(this);
    InitWidget();

    connect(ui->tableWidget,&QTableWidget::cellDoubleClicked,this,&Widget::musicplayer);
    connect(ui->horizontalSlider,&QSlider::sliderMoved,this,&Widget::setPositon);
    connect(player, &QMediaPlayer::durationChanged, this, &Widget::updateDuration);
    connect(player,&QMediaPlayer::positionChanged,this,&Widget::valueChanged);

}

void Widget::InitWidget()
{
    ui->label_2->hide();
    ui->label_3->hide();
    ui->tableWidget->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_7->hide();
    ui->pushButton_8->hide();
    ui->horizontalSlider->hide();
    ui->label_4->hide();
}

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

void Widget::on_pushButton_clicked()
{
    ui->label_2->show();
    ui->tableWidget->show();
    ui->pushButton_5->show();
}

void Widget::on_pushButton_5_clicked()
{
    path = QFileDialog::getExistingDirectory(this,"请选择本地音乐文件夹");
    std::vector<QString> fileNames;
    findFile(path,fileNames);

    //设置行数,列数
    ui->tableWidget->setColumnCount(1);
    ui->tableWidget->setRowCount(fileNames.size());

    num_music = fileNames.size();

    for(int i=0;i<fileNames.size();i++)
    {

        ui->tableWidget->setItem(i,0,new QTableWidgetItem(fileNames[i]));
        //设置cell不可编辑
        QTableWidgetItem* pItem = ui->tableWidget->item(i, 0);
        int editflag = Qt::ItemIsEditable;
        pItem->setFlags((Qt::ItemFlag)(~editflag & pItem->flags()));
    }
}

static QString formatTime(qint64 timeMilliSeconds)
{
    qint64 seconds = timeMilliSeconds / 1000;
    const qint64 minutes = seconds / 60;
    seconds -= minutes * 60;
    return QStringLiteral("%1:%2")
        .arg(minutes, 2, 10, QLatin1Char('0'))
        .arg(seconds, 2, 10, QLatin1Char('0'));
}

void Widget::musicplayer(int row, int column)
{
    QTableWidgetItem *item = new QTableWidgetItem;

    playlist->clear();
    player->pause();
        item = ui->tableWidget->item(row,0);
//        player->setMedia(QUrl::fromLocalFile(item->text()));


        for(int i=row;i<num_music;i++)
        {
            item = ui->tableWidget->item(i,0);
            playlist->addMedia(QUrl::fromLocalFile(item->text()));
        }
        playlist->setCurrentIndex(0);
        playlist->setPlaybackMode(QMediaPlaylist::Loop);
        player->setPlaylist(playlist);
        player->play();

        ui->label_3->show();
        ui->pushButton_6->show();
        ui->pushButton_7->show();
        ui->pushButton_8->show();
        ui->horizontalSlider->show();
        ui->label_4->setText(formatTime(0));
        ui->label_4->show();
}



void Widget::findFile(const QString &path, std::vector<QString> &fileNames)
{
    QDir dir(path);

    //获取path下所有文件夹和文件
    dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot); //文件夹/文件/不包含./和../

    //排序文件夹优先
    dir.setSorting(QDir::DirsFirst);

    //获取文件夹下所有文件(文件夹+文件)
    QFileInfoList list = dir.entryInfoList();

    if(list.size()==0)
    {
        return;
    }

    //遍历
    for(int i=0;i<list.size();i++)
    {
        QFileInfo fileInfo = list.at(i);
        if(fileInfo.isDir())
        {
            findFile(fileInfo.filePath(),fileNames);
        }
        else {
            if(fileInfo.suffix()=="mp3")
            {
                fileNames.emplace_back(list.at(i).filePath());
            }
        }
    }
}

void Widget::on_pushButton_7_clicked()
{
    playlist->next();
}

void Widget::on_pushButton_8_clicked()
{
    playlist->previous();
}

void Widget::on_pushButton_6_clicked(bool checked)
{
    int status1 = status%2;
    //qDebug()<
    if(status1==0)
    {
        player->setPosition(position);
        player->play();
        ui->pushButton_6->setIcon(QPixmap("C:/Users/11830/Downloads/4.jpg"));
    }
    else {
        player->pause();
        position = player->position();
        ui->pushButton_6->setIcon(QPixmap("C:/Users/11830/Downloads/1.jpg"));
    }
    status+=1;
}

void Widget::valueChanged(qint64 value)
{
    ui->horizontalSlider->setValue(value);
    ui->label_4->setText(formatTime(value));
    ui->label_3->setText(player->metaData("Title").toString());
}

void Widget::setPositon(int value)
{
    if (qAbs(player->position() - value) > 99)
        player->setPosition(value);
    //qDebug()<<(value*player->duration()/100);
}

void Widget::updateDuration(qint64 duration)
{
    ui->horizontalSlider->setRange(0, duration);
    ui->horizontalSlider->setEnabled(duration > 0);
    ui->horizontalSlider->setPageStep(duration / 10);
}

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

你可能感兴趣的:(#,qt,qt)