QT音乐播放器实战项目资源
立即下载
资源介绍:
在本QT音乐播放器实战项目中,实现了音乐播放器的基本功能,例如开始播放、停止播放、下一首播放、上一首播放、调节音量、调节倍速、设置音乐播放模式等。同时还具备搜索功能,通过搜索歌曲名字或歌手名字来获取歌曲,实现播放网上音乐。
该QT实战项目代码量在600~700之间,适合QT学者拿来练习,增加项目经验。
该资源包含了该项目的全部源代码和项目中用到的资源文件。项目的效果演示在我的文章和视频中有展现,感兴趣的可以去看一下效果。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#define DEFINE_WIDTH 1020 //窗体宽度
#define DEFINE_HEIGHT 600 //窗体高度
#define REQUEST_PATH "https://music.cyrilstudio.top/search?keywords=" //搜索歌手/歌曲路径
#define NETWORK_MUSIC_PATH "https://music.163.com/song/media/outer/url?id=" //歌曲播放路径
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("我的音乐播放器");
setFixedSize(DEFINE_WIDTH,DEFINE_HEIGHT);
setWindowIcon(QIcon(":/Resources/images/musicIcon.ico"));
m_curVolumeVal = 50; //初始化当前音量为50
m_curRate = 1.0; //初始化当前倍速为1.0x
m_curMusicName = new QLabel(this);
m_curMusicVolume = new QLabel(this);
m_musicStatus = new QLabel(this);
m_networkPlayList = new QMediaPlaylist(this);
m_networkPlayList->setPlaybackMode(QMediaPlaylist::Loop);
m_localPlayList = new QMediaPlaylist(this);
m_localPlayList->setPlaybackMode(QMediaPlaylist::Loop);
addLocalSongs(); //将本地音乐添加到播放列表
m_player = new QMediaPlayer(this);
m_player->setPlaylist(m_localPlayList); //设置播放器播放列表
m_player->setVolume(m_curVolumeVal); //设置播放器音量
m_player->setMedia(m_localPlayList); //设置播放器音乐
connect(m_player,&QMediaPlayer::durationChanged,this,&MainWindow::onMusicTotalTimeChanged);
connect(m_player,&QMediaPlayer::positionChanged,this,&MainWindow::onMusicCurrentTimeChanged);
connect(m_player,&QMediaPlayer::mediaStatusChanged,this,&MainWindow::onMediaStatusChanged);
m_NetAccessManger = new QNetworkAccessManager(this);
connect(m_NetAccessManger,&QNetworkAccessManager::finished,this,&MainWindow::onReplied);
init(); //初始化
}
MainWindow::~MainWindow()
{
delete ui;
}
//初始化
void MainWindow::init()
{
initSub(); //初始化组件
initStatusBar(); //初始化状态栏
initBtnPlayMode(); //初始化播放模式按钮
}
//初始化组件
void MainWindow::initSub()
{
//初始化下拉框
QStringList speedList;
speedList<<"0.5x"<<"1.0x"<<"1.25x"<<"1.75x"<<"2.0x";
ui->comboBoxSpeed->addItems(speedList);
ui->comboBoxSpeed->setCurrentIndex(1);
//初始化音量
ui->horizontalSliderVolume->setMinimum(0);
ui->horizontalSliderVolume->setMaximum(100);
ui->horizontalSliderVolume->setValue(m_curVolumeVal);
//初始化音乐当前时间和总时间标签
ui->labelCurrentTime->setText("00:00");
ui->labelTotalTime->setText("未知");
//初始化listwidgetLocalList
QStringList localList;
localList<<"纯音乐1"<<"像风一样自由"<<"Areyouok";
ui->listWidgetLocalList->clear();
for(int i=0;ilistWidgetLocalList->addItem(item);
if(i == 0)
{
ui->listWidgetLocalList->setCurrentItem(item);
}
}
//初始化歌词行
QPalette pal;
pal.setColor(QPalette::Text,QColor(Qt::blue));
ui->lineEditLyrics->setPalette(pal);
}
//初始化状态栏
void MainWindow::initStatusBar()
{
ui->statusbar->setStyleSheet("margin-right: 20px;"); //状态栏中的所有小部件设置右边距为20px
//初始化标签
m_curMusicName->setText("当前歌曲:未知");
m_curMusicVolume->setText(QString("当前音量:%1").arg(m_curVolumeVal));
m_musicStatus->setText("音乐状态:正常");
//向状态栏中添加标签
ui->statusbar->addWidget(m_curMusicName);
ui->statusbar->addWidget(m_curMusicVolume);
ui->statusbar->addWidget(m_musicStatus);
}
//初始化播放模式按钮
void MainWindow::initBtnPlayMode()
{
QMediaPlaylist* tempList = m_player->playlist();
if(tempList->playbackMode() == QMediaPlaylist::Loop)
{
ui->pushButtonPlayMode->setText("顺序播放");
}
else if(tempList->playbackMode() == QMediaPlaylist::Random)
{
ui->pushButtonPlayMode->setText("随机播放");
}
else if(tempList->playbackMode() == QMediaPlaylist::CurrentItemInLoop)
{
ui->pushButtonPlayMode->setText("单曲循环");
}
}
//更新播放倍速
void MainWindow::updateRate()
{
int count = ui->comboBoxSpeed->count(); //获取倍速下拉框的item数目
for(int i = 0;icomboBoxSpeed->itemText(i); //获取item文本
int strSize = str.size();
qreal rate = str.left(strSize - 1).toFloat(); //例如:倍速1.25x,把x去掉,然后转为float类型
if(rate == m_curRate)
{
ui->comboBoxSpeed->setCurrentIndex(i); //恢复倍速下拉框
m_player->setPlaybackRate(m_curRate); //恢复本地音乐播放倍速
return;
}
}
}
//将本地音乐添加到本地播放列表
void MainWindow::addLocalSongs()
{
m_localPlayList->clear();
m_localPlayList->addMedia(QUrl("qrc:/Resources/sound/background.mp3"));
m_localPlayList->addMedia(QUrl("qrc:/Resources/sound/freelikewind.mp3"));
m_localPlayList->addMedia(QUrl("qrc:/Resources/sound/Areyouok.mp3"));
m_localPlayList->setCurrentIndex(0);
}
//歌曲总时间改变槽函数
void MainWindow::onMusicTotalTimeChanged(qint64 totalTime)
{
//更新歌曲总时间
int totalSec = totalTime / 1000; //毫秒换算成秒
int m = totalSec / 60; //获取分钟
int s = totalSec % 60; //获取秒
//设置labelTotalTime文本
ui->labelTotalTime->setText(QString("%1:%2").arg(m,2,10,QChar('0')).arg(s,2,10,QChar('0')));
ui->horizontalSliderTime->setMaximum(totalTime); //设置音乐时间条的最大值为总时间的值
//更新listwidget和状态栏中的标签内容
QListWidgetItem* item = nullptr;
QMediaPlaylist* tempList = m_player->playlist();
if(tempList == m_localPlayList)
{
//更新listWidgetLocalList当前选中的子项
int curIndex = tempList->currentIndex();
item = ui->listWidgetLocalList->item(curIndex);
ui->listWidgetLocalList->setCurrentItem(item);
//更新歌曲名显示标签
QString musicName = item->text();
m_curMusicName->setText("当前歌曲:"+musicName);
}
else if(tempList == m_networkPlayList)
{
//更新listWidgetNetList当前选中的子项
int curIndex = tempList->currentIndex();
item = ui->listWidgetNetList->item(curIndex);
ui->listWidgetNetList->setCurrentItem(item);
//更新歌曲名显示标签
QString musicName = m_VecMusicInfos[curIndex].getMusicName() + " " +
m_VecMusicInfos[curIndex].getMusicSinger();
m_curMusicName->setText("当前歌曲:"+musicName);
}
}
//更新音乐播放的当前时间进度槽函数
void MainWindow::onMusicCurrentTimeChanged(qint64 curTime)
{
int totalSec = curTime / 1000;
int m = totalSec / 60;
int s = totalSec % 60;
ui->labelCurrentTime->setText(QString("%1:%2").arg(m,2,10,QChar('0')).arg(s,2,10,QChar('0')));
ui->horizontalSliderTime->setValue(curTime);
}
//媒体状态发生改变槽函数
void MainWindow::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
if(status == QMediaPlayer::InvalidMedia)
{
ui->statusbar->showMessage("音乐状态:无法播放",1000);
}
else if(status == QMediaPlayer::LoadingMedia)
{
m_musicStatus->setText("音乐状态:�