#include "music.h"
int Music::z = 2;//静态变量控制播放方式
Music::Music(QWidget *parent)
: QWidget(parent)
{
QPixmap backgroud;
backgroud.load("/home/pi/CPP/exp_12/music/map.png");
this->resize(backgroud.width(), backgroud.height());//设置窗口和背景图片大小一致
this->setWindowFlags(Qt::FramelessWindowHint);
add = false;//设置初始值
moved = 0;
timer = new QTimer(this);
timer2 = new QTimer(this);
player = new QMediaPlayer(this);
playList = new QMediaPlaylist(this);
init_controls();//创建按钮
// init_skin();//外部加载 qss 文件,绘制界面样式
connect(player, SIGNAL(metaDataAvailableChanged(bool)), this,SLOT(showMessage(bool)));
connect(seekSlider, SIGNAL(sliderMoved(int)), this,SLOT(seekChange(int)));
}
Music::~Music()
{
}
void Music::addMoreMusic()
{
QString songFileName, songName;
QFileInfo info;
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("打开文件"), "", tr("music(*.mp3)"));
if (!fileNames.isEmpty())
add = true;
for (int i=0; i<fileNames.size(); i++)
{
playList->addMedia(QUrl::fromLocalFile(fileNames.at(i)));//将音乐文件逐一加入播放列表
songFileName = fileNames.at(i);
info = QFileInfo(songFileName);
songName = info.fileName();
list->addItem(songName);
message->setText(tr("添加成功"));
}
playList->setCurrentIndex(0);
if (z == 0)
playList->setPlaybackMode(QMediaPlaylist::Random);
else if (z == 1)
playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
else
playList->setPlaybackMode(QMediaPlaylist::Sequential);
player->setPlaylist(playList);
// BtnPlay->setStyleSheet("QPushButton:!hover{border-image:url(:/images/play_hover.png);}"
// "\QPushButton:hover{border-image:url(:/images/play_hover.png);}"
// "\QPushButton:pressed{border-image:url(:/images/play_press.png);}");
connect(timer2, SIGNAL(timeout()), this, SLOT(clearMessage()));//使用 clearMessage()函数来清除上面显示的“ 添加成功”
timer2->start(2000);
}
void Music::playMusic()
{
if (!add)
QMessageBox::information(this, "添加音乐", "请先添加音乐", "确定");
else
{
if (player->state() == QMediaPlayer::PlayingState)//判断是否为播放模式
{
//设置播放模式为暂停
player->pause();
timer->stop();
//改变播放按钮的背景图片
// BtnPlay->setStyleSheet("QPushButton:!hover{border-image:url(:/images/play_hover.png);}"
// "\QPushButton:hover{border-image:url(:/images/play_hover.png);}"
// "\QPushButton:pressed{border-image:url(:/images/play_press.png);}");
}
else
{
//否则设置播放模式
player->play();
connect(timer, SIGNAL(timeout()), this,
SLOT(posChange()));
timer->start(1000);
// BtnPlay->setStyleSheet("QPushButton:!hover{border-image:url(:/images/pause_hover.png);}"
// "\QPushButton:hover{border-image:url(:/images/pause_hover.png);}"
// "\QPushButton:pressed{border-image:url(:/images/pause_press.png);}");
}
}
}
void Music::preMusic()
{
//上一首,并更新播放时间为 0
moved = 0;
playList->previous();
}
void Music::nextMusic()
{
//下一首,更新播放时间为 0
moved = 0;
playList->next();
}
void Music::meteOpen()
{
static bool flag = true;//判断是否已点击
if (flag)
{
// muteButton->setStyleSheet("QPushButton:!hover{border-image:url(:/images/sound_close.png)}"
// "\QPushButton:hover{border-image:url(:/images/sound_close.png)}"
// "\QPushButton:pressed{border-image:url(:/images/sound_close.png)}"
// "\QPushButton:focus{padding:-1;}");
muteButton->setToolTip(tr("打开声音"));
player->setMuted(true);
}
else
{
// muteButton->setStyleSheet("QPushButton:!hover{border-image:url(:/images/sound.png)}"
// "\QPushButton:hover{border-image:url(:/images/sound.png)}"
// "\QPushButton:pressed{border-image:url(:/images/sound.png)}"
// "\QPushButton:focus{padding:-1;}");
muteButton->setToolTip(tr("关闭声音"));
player->setMuted(false);
}
flag = !flag;
}
void Music::volumChange(int vol)
{
player->setVolume(vol);//关联滑块的移动,设置播放的音量
}
void Music::positionChange(qint64 position)
{
seekSlider->setMaximum(player->duration()/1000);//设置滑块的长度范围为音乐长.00 度
seekSlider->setValue(position/1000);//如果音乐进度改变,则滑块显示位置
moved = position;
QTime moveTime(0,(moved/60000)%60, (moved/1000)%60);//设置时间
showPro->setText(moveTime.toString("mm:ss"));
}
void Music::showMessage(bool ok)
{
if (ok)
{
QString name = player->metaData(QMediaMetaData::Title).toString();
QString author = player->metaData(QMediaMetaData::Author).toString();
qDebug()<<"name:"<<name<<"\n"<<"author:"<<author;
showMge->setText("正在播放: "+name+"-"+author);
QTime displayTime(0,(player->duration()/60000)%60,
(player->duration()/1000)%60);
showTime->setText(displayTime.toString("mm:ss"));
}
}
void Music::seekChange(int position)//滑动条快进、倒退音乐,且音乐已播放时长也跟着变化
{
player->setPosition(position *1000);//如果滑块改变位置,则改变音乐进度
moved = position *1000;//让播放时间等于音乐进度(毫秒)
QTime moveTime(0, (moved/60000)%60, (moved/1000)%60);//设置时间
showPro->setText(moveTime.toString("mm:ss"));
}
void Music::posChange()
{
if (moved >= player->duration())
moved = 0;
}
void Music::clearMessage()
{
message->setText("");
}
void Music::musicPlayPattern()
{
if (z == 0)
{
// playPattern->setStyleSheet("QPushButton:!hover{border-image:url(:/images/Seq.png);}"
// "\QPushButton:hover{border-image:url(:/images/Seq.png);}"
// "\QPushButton:pressed{border-image:url(:/images/Seq.png);}");
playPattern->setToolTip(tr("列表循环"));
playList->setPlaybackMode(QMediaPlaylist::Sequential);
z = 2;
}
else if (z == 1)
{
// playPattern->setStyleSheet("QPushButton:!hover{border-image:url(:/images/Ran.png);}"
// "\QPushButton:hover{border-image:url(:/images/Ran.png);}"
// "\QPushButton:pressed{border-image:url(:/images/Ran.png);}");
playPattern->setToolTip(tr("随机播放"));
playList->setPlaybackMode(QMediaPlaylist::Random);
z--;
}
else
{
// playPattern->setStyleSheet("QPushButton:!hover{border-image:url(:/images/Single.png);}"
// "\QPushButton:hover{border-image:url(:/images/Single.png);}"
// "\QPushButton:pressed{border-image:url(:/images/Single.png);}");
playPattern->setToolTip(tr("单曲循环"));
playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
z--;
}
}
void Music::init_controls()
{
BtnClose = new QPushButton(this);
BtnClose->setObjectName("BtnClose");//如果要对这个对象单独设stylesheet 的话一定要设置它的 objectname
BtnClose->setGeometry(365, 2, 30, 30);//窗口左上角为原点( X365,Y2),宽 30,高 30
BtnClose->setToolTip(tr("退出"));
BtnClose->setCursor(QCursor(Qt::PointingHandCursor));//鼠标指针形状为手
BtnMin = new QPushButton(this);
BtnMin->setObjectName(tr("BtnMin"));
BtnMin->setGeometry(330,5,25,30);
BtnMin->setToolTip(tr("最小化"));
BtnMin->setCursor(QCursor(Qt::PointingHandCursor));
playPattern = ne