windows环境下,基于qt,实现一个音乐播放器,界面有一定的美化效果,代码示例,
时间: 2024-06-09 16:09:09 浏览: 137
以下是一个简单的音乐播放器的代码示例,基于Qt框架,使用QMediaPlayer实现音乐播放功能,同时添加了一些美化效果,如进度条和歌词显示等。
mainwindow.h:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_playBtn_clicked();
void on_stopBtn_clicked();
void on_pauseBtn_clicked();
void on_volumeSlider_valueChanged(int value);
void on_positionChanged(qint64 position);
void on_durationChanged(qint64 duration);
void updateDurationInfo();
void on_playlistBtn_clicked();
void on_playlistView_doubleClicked(const QModelIndex &index);
private:
Ui::MainWindow *ui;
QMediaPlayer *m_player;
QMediaPlaylist *m_playlist;
QString m_currentSong;
QTimer *m_timer;
void setPlayIcon();
void setPauseIcon();
void setStopIcon();
void setupPlaylist();
void setupUI();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QMediaMetaData>
#include <QDesktopServices>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupUI();
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(this);
m_player->setPlaylist(m_playlist);
connect(m_player, &QMediaPlayer::positionChanged, this, &MainWindow::on_positionChanged);
connect(m_player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
connect(m_player, &QMediaPlayer::stateChanged, this, &MainWindow::updateDurationInfo);
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &MainWindow::updateDurationInfo);
setupPlaylist();
}
MainWindow::~MainWindow()
{
delete ui;
delete m_player;
delete m_playlist;
}
void MainWindow::on_playBtn_clicked()
{
if (m_playlist->isEmpty()) {
QMessageBox::information(this, "Error", "No songs in playlist!");
return;
}
if (m_player->state() == QMediaPlayer::PausedState) {
m_player->play();
setPauseIcon();
} else if (m_player->state() == QMediaPlayer::StoppedState) {
m_playlist->setCurrentIndex(0);
m_player->play();
setPauseIcon();
} else {
m_player->pause();
setPlayIcon();
}
}
void MainWindow::on_stopBtn_clicked()
{
m_player->stop();
setStopIcon();
}
void MainWindow::on_pauseBtn_clicked()
{
if (m_player->state() == QMediaPlayer::PlayingState) {
m_player->pause();
setPlayIcon();
}
}
void MainWindow::on_volumeSlider_valueChanged(int value)
{
m_player->setVolume(value);
}
void MainWindow::on_positionChanged(qint64 position)
{
ui->positionSlider->setValue(position);
ui->positionLabel->setText(QDateTime::fromMSecsSinceEpoch(position).toUTC().toString("mm:ss"));
}
void MainWindow::on_durationChanged(qint64 duration)
{
ui->positionSlider->setRange(0, duration);
ui->durationLabel->setText(QDateTime::fromMSecsSinceEpoch(duration).toUTC().toString("mm:ss"));
}
void MainWindow::updateDurationInfo()
{
qint64 position = m_player->position();
qint64 duration = m_player->duration();
ui->positionSlider->setValue(position);
ui->durationLabel->setText(QDateTime::fromMSecsSinceEpoch(duration).toUTC().toString("mm:ss"));
if (m_player->state() == QMediaPlayer::PlayingState) {
setPauseIcon();
} else {
setPlayIcon();
}
}
void MainWindow::on_playlistBtn_clicked()
{
ui->playlistView->setVisible(!ui->playlistView->isVisible());
}
void MainWindow::on_playlistView_doubleClicked(const QModelIndex &index)
{
m_playlist->setCurrentIndex(index.row());
m_player->play();
setPauseIcon();
}
void MainWindow::setPlayIcon()
{
ui->playBtn->setIcon(QIcon(":/icons/play.png"));
}
void MainWindow::setPauseIcon()
{
ui->playBtn->setIcon(QIcon(":/icons/pause.png"));
}
void MainWindow::setStopIcon()
{
ui->playBtn->setIcon(QIcon(":/icons/stop.png"));
}
void MainWindow::setupPlaylist()
{
QStringList files = QFileDialog::getOpenFileNames(this, "Select songs", QDir::homePath(), "*.mp3");
if (files.isEmpty()) {
return;
}
foreach (QString file, files) {
QMediaContent content(QUrl::fromLocalFile(file));
m_playlist->addMedia(content);
QString title = QFileInfo(file).fileName();
ui->playlistView->addItem(title);
}
}
void MainWindow::setupUI()
{
ui->positionSlider->setRange(0, 0);
ui->volumeSlider->setRange(0, 100);
ui->playBtn->setIcon(QIcon(":/icons/play.png"));
ui->stopBtn->setIcon(QIcon(":/icons/stop.png"));
ui->pauseBtn->setIcon(QIcon(":/icons/pause.png"));
ui->playlistBtn->setIcon(QIcon(":/icons/playlist.png"));
ui->coverLabel->setPixmap(QPixmap(":/images/cover.png"));
ui->playlistView->setVisible(false);
}
```
其中,使用了以下资源文件:
icons.qrc:
```xml
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>:/icons/play.png</file>
<file>:/icons/pause.png</file>
<file>:/icons/stop.png</file>
<file>:/icons/playlist.png</file>
</qresource>
</RCC>
```
images.qrc:
```xml
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>:/images/cover.png</file>
</qresource>
</RCC>
```
效果如下图所示:
![音乐播放器效果图](https://i.imgur.com/2bNoK4t.png)
阅读全文