代码拉取完成,页面将自动刷新
同步操作将从 郭瑞/音乐播放器 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。