-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusicplayer.cpp
More file actions
223 lines (190 loc) · 7.21 KB
/
musicplayer.cpp
File metadata and controls
223 lines (190 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "musicplayer.h"
#include <QMediaPlaylist>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileInfo>
#include "currentdevicemanager.h"
MusicPlayer::MusicPlayer(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *mainVBLayout = new QVBoxLayout(this);
QHBoxLayout *volumeLayout = new QHBoxLayout;
QHBoxLayout *positionLayout = new QHBoxLayout;
QHBoxLayout *buttonsLayout = new QHBoxLayout;
player = getPlayer();
nameLabel = new QLabel(this);
DSlider *volumeSlider = new DSlider(Qt::Horizontal, this);
volumeSlider->setFixedWidth(130);
volumeSlider->setMaximum(100);
volumeLabel = new QLabel(this);
volumeLabel->setFixedSize(15, 15);
volumeLabel->setPixmap(QPixmap(":/images/volume.png"));
volumeLabel->setScaledContents(true);
QLabel *volumeNumLabel = new QLabel(this);
positionSlider = new DSlider(Qt::Horizontal, this);
positionSlider->setFixedWidth(130);
durationLabel = new QLabel(this);
positionLabel = new QLabel(this);
resetMusicInfo();
playButton = new QPushButton(this);
QPushButton *stopButton = new QPushButton(this);
QPushButton *nextButton = new QPushButton(this);
QPushButton *preButton = new QPushButton(this);
playButton->setIcon(QIcon(":/images/buttonPlay.png"));
stopButton->setIcon(QIcon(":/images/buttonStop.png"));
nextButton->setIcon(QIcon(":/images/buttonNext.png"));
preButton->setIcon(QIcon(":/images/buttonPre.png"));
connect(CurrentDeviceManager::getInstance(), &CurrentDeviceManager::deviceUnMounted, this, &MusicPlayer::stopPlay);
connect(player, &QMediaPlayer::mediaStatusChanged, this, &MusicPlayer::setMusicName);
connect(volumeSlider, &DSlider::valueChanged, player, &QMediaPlayer::setVolume);
connect(player, &QMediaPlayer::volumeChanged, volumeNumLabel, static_cast<void(QLabel::*)(int)>(&QLabel::setNum));
connect(player, &QMediaPlayer::volumeChanged, this, &MusicPlayer::onVolumeChanged);
connect(positionSlider, &DSlider::sliderPressed, this, &MusicPlayer::onPositionSliderPressed);
connect(positionSlider, &DSlider::sliderReleased, this, &MusicPlayer::onPositionSliderReleased);
connect(player, &QMediaPlayer::durationChanged, this, &MusicPlayer::setDurationLabel);
connect(player, &QMediaPlayer::positionChanged, this, &MusicPlayer::setPositionLabel);
connect(player, &QMediaPlayer::durationChanged, positionSlider, &DSlider::setMaximum);
connect(player, &QMediaPlayer::positionChanged, positionSlider, &DSlider::setValue);
connect(player, &QMediaPlayer::stateChanged, this, &MusicPlayer::onPlayStateChanged);
connect(playButton, &QPushButton::clicked, this, &MusicPlayer::playPause);
connect(stopButton, &QPushButton::clicked, this, &MusicPlayer::stopPlay);
connect(nextButton, &QPushButton::clicked, this, &MusicPlayer::playNext);
connect(preButton, &QPushButton::clicked, this, &MusicPlayer::playPre);
volumeLayout->setContentsMargins(0, 0, 0, 0);
volumeLayout->addWidget(volumeSlider);
volumeLayout->addWidget(volumeLabel);
volumeLayout->addWidget(volumeNumLabel);
positionLayout->setContentsMargins(0, 0, 0, 0);
positionLayout->addWidget(positionSlider);
positionLayout->addWidget(positionLabel);
positionLayout->addWidget(durationLabel);
buttonsLayout->addStretch(1);
buttonsLayout->addWidget(preButton);
buttonsLayout->addWidget(playButton);
buttonsLayout->addWidget(stopButton);
buttonsLayout->addWidget(nextButton);
buttonsLayout->addStretch(1);
mainVBLayout->addWidget(nameLabel);
mainVBLayout->addLayout(volumeLayout);
mainVBLayout->addLayout(positionLayout);
mainVBLayout->addLayout(buttonsLayout);
this->setLayout(mainVBLayout);
// 设置一个空的播放列表,否则当第一次初始化MusicPlayer控件后点击上一曲或下一曲程序将会崩溃
player->setPlaylist(new QMediaPlaylist(this));
player->setVolume(50);
volumeSlider->setValue(50);
}
QMediaPlayer *MusicPlayer::getPlayer()
{
static QMediaPlayer s_player;
return &s_player;
}
// 卸载设备前需要先清空playlist, 否则会因为资源占用导致卸载设备失败
void MusicPlayer::stopAndClear()
{
getPlayer()->stop();
getPlayer()->playlist()->clear();
}
void MusicPlayer::playPause()
{
switch (player->state()) {
case QMediaPlayer::PlayingState:
player->pause();
break;
case QMediaPlayer::PausedState:
player->play();
break;
default:
break;
}
}
void MusicPlayer::stopPlay()
{
if (player->state() != QMediaPlayer::StoppedState) {
player->stop();
}
}
void MusicPlayer::playNext()
{
player->pause();
QMediaPlaylist *playList = player->playlist();
playList->next();
player->play();
}
void MusicPlayer::playPre()
{
player->pause();
QMediaPlaylist *playList = player->playlist();
playList->previous();
player->play();
}
void MusicPlayer::onPlayStateChanged(QMediaPlayer::State state)
{
switch (state) {
case QMediaPlayer::PlayingState:
playButton->setIcon(QIcon(":/images/buttonPause.png"));
break;
case QMediaPlayer::PausedState:
playButton->setIcon(QIcon(":/images/buttonPlay.png"));
break;
case QMediaPlayer::StoppedState:
playButton->setIcon(QIcon(":/images/buttonPlay.png"));
resetMusicInfo();
break;
default:
break;
}
}
void MusicPlayer::setMusicName(QMediaPlayer::MediaStatus status)
{
if (status == QMediaPlayer::BufferedMedia) {
QString title = player->metaData("Title").toString();
if (title.isEmpty()) {
qDebug() << "(MusicPalyer::setMusicName) get title metadata failed, set title to file name!";
title = QFileInfo(player->currentMedia().canonicalUrl().fileName()).completeBaseName();
}
nameLabel->setText(title);
qDebug() << "(MusicPalyer::setMusicName) playing music:" << title;
}
}
void MusicPlayer::setDurationLabel(qint64 duration)
{
if (duration != 0) {
positionSlider->setMaximum(duration);
int durationS = duration / 1000;
int seconds = durationS % 60;
int minutes = (durationS - seconds) / 60;
durationLabel->setText(QString::number(minutes) + ":" + QString::number(seconds));
}
}
void MusicPlayer::setPositionLabel(qint64 position)
{
if (position != 0) {
int positionS = position / 1000;
int seconds = positionS % 60;
int minutes = (positionS - seconds) / 60;
positionLabel->setText(QString::number(minutes) + ":" + QString::number(seconds));
}
}
void MusicPlayer::onPositionSliderReleased()
{
connect(player, &QMediaPlayer::positionChanged, positionSlider, &DSlider::setValue);
player->setPosition(positionSlider->value());
}
void MusicPlayer::onPositionSliderPressed()
{
disconnect(player, &QMediaPlayer::positionChanged, positionSlider, &DSlider::setValue);
}
void MusicPlayer::onVolumeChanged(int volume)
{
if (volume == 0) {
volumeLabel->setPixmap(QPixmap(":/images/volumeMute.png"));
} else {
volumeLabel->setPixmap(QPixmap(":/images/volume.png"));
}
}
void MusicPlayer::resetMusicInfo()
{
nameLabel->setText("waiting for play...");
durationLabel->setText("0:0");
positionLabel->setText("0:0");
}