1 Star 0 Fork 2

huachang/音乐播放器

forked from 郭瑞/音乐播放器 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CMusicWidget.cpp 4.18 KB
一键复制 编辑 原始数据 按行查看 历史
郭瑞 提交于 2021-07-14 07:09 . Signed-off-by: guorui 2573383280@qq.com
#include "CMusicWidget.h"
#include "ui_CMusicWidget.h"
CMusicWidget::CMusicWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::CMusicWidget)
{
ui->setupUi(this);
readFile();
initUI();
}
CMusicWidget::~CMusicWidget()
{
delete ui;
}
void CMusicWidget::readFile()
{
// 设置遍历目录
str_pathmusic = "C:/Users/lenovo/Music/";
QDir dir(str_pathmusic);
// 设置文件过滤器
QStringList nameFilters;
// 设置文件过滤器的格式
nameFilters << "*.mp3";
// 将过滤后的文件名存入list中
QStringList files = dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
// 添加到listwidget中的item中
ui->listWidget->addItems(files);
// 把item加入到listwidget中
ui->listWidget->sortItems();
// 设置默认item
p_item = ui->listWidget->item(0);
p_item->setSelected(true);
n_music_number = 0;
}
// 封装UI初始化,避免在构造中使用
void CMusicWidget::initUI()
{
connect(ui->btnUp, SIGNAL(clicked()), this, SLOT(slot_btnUp_clicked()));
connect(ui->btnPause, SIGNAL(clicked()), this, SLOT(slot_btnPause_clicked()));
connect(ui->btnDown, SIGNAL(clicked()), this, SLOT(slot_btnDown_clicked()));
connect(ui->listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(slot_musicListRowNum(int)));
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slot_musicListRowNum(QListWidgetItem*)));
b_play_music = false;
p_player = nullptr;
p_player = new QMediaPlayer;
if(p_player == nullptr)
qDebug()<<"initUI:p_player分配失败!"<<endl;
}
void CMusicWidget::slot_musicListRowNum(int n)
{
// qDebug()<<n<<endl;
// qDebug()<<ui->listWidget->item(n)->text()<<endl;
if(n < 0 || n >= ui->listWidget->count())
return ;
if(b_play_music)
p_player->stop();
n_music_number = n;
str_music = ui->listWidget->item(n)->text();
p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
if(b_play_music)
p_player->play();
}
void CMusicWidget::slot_musicListRowNum(QListWidgetItem* p_n)
{
p_item = p_n;
p_item->setSelected(true);
n_music_number = ui->listWidget->currentRow();// 获取下标
str_music = p_item->text();
p_player->stop();
p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
p_player->play();
ui->btnPause->setStyleSheet("QPushButton#btnPause{background-image: url(:/res/start.png);}");
b_play_music = true;
}
void CMusicWidget::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QStyleOption opt;
opt.init(this);
QPainter pt(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &pt, this);
}
void CMusicWidget::slot_btnUp_clicked()
{
if(b_play_music)
p_player->stop();
if(n_music_number == 0)
{
int nMax = ui->listWidget->count();
n_music_number = nMax - 1;// 设置为最后一曲
}
else
{
n_music_number--;
}
str_music = ui->listWidget->item(n_music_number)->text();
p_item = ui->listWidget->item(n_music_number);
p_item->setSelected(true);// 设置选中
p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
if(b_play_music)
p_player->play();
}
void CMusicWidget::slot_btnPause_clicked()
{
if(b_play_music)
{
ui->btnPause->setStyleSheet("QPushButton#btnPause{background-image: url(:/res/pause.png);}");
b_play_music = false;
p_player->pause();
}
else
{
ui->btnPause->setStyleSheet("QPushButton#btnPause{background-image: url(:/res/start.png);}");
b_play_music = true;
p_player->play();
}
}
void CMusicWidget::slot_btnDown_clicked()
{
if(b_play_music)
p_player->stop();
int nMax = ui->listWidget->count();
if(n_music_number == nMax - 1)
{
n_music_number = 0;// 设置为最后一曲
}
else
{
n_music_number++;
}
str_music = ui->listWidget->item(n_music_number)->text();
p_item = ui->listWidget->item(n_music_number);
p_item->setSelected(true);// 设置选中
p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
if(b_play_music)
p_player->play();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chang_devel/music-player.git
git@gitee.com:chang_devel/music-player.git
chang_devel
music-player
音乐播放器
master

搜索帮助