您好,登錄后才能下訂單哦!
這篇文章主要介紹“Qt如何實現MP3音樂播放器”,在日常操作中,相信很多人在Qt如何實現MP3音樂播放器問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Qt如何實現MP3音樂播放器”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
首先通過打開文件,獲取文件夾下mp3文件的名字,將其存入QTableWidget控件下,通過雙擊QTableWidget空間中的內容,播放相應的mp3音樂文件,同時通過使用QMediaPlaylist來實現上一首,下一首的功能。
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> 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<QFileDialog> #include<QDebug> #include<QMediaPlayer> #include<QTableWidgetItem> #include<QMouseEvent> #include<QMediaPlaylist> 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()<<checked; 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 <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
到此,關于“Qt如何實現MP3音樂播放器”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。