-
Notifications
You must be signed in to change notification settings - Fork 0
/
myplayer.cpp
405 lines (322 loc) · 11.7 KB
/
myplayer.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "myplayer.h"
#include "ui_myplayer.h"
#include "mymediaplayer.h"
#include "mymetadata.h"
#include <QDebug>
#include <QMediaContent>
#include <QUrl>
#include <QString>
#include <QObject>
#include <QTime>
#include <QStringList>
#include <QFileInfo>
#include <QDir>
#include <QmessageBox>
#include <QPixmap>
#include <QFileDialog>
#include <QStandardPaths>
#include <QIcon>
#include <QCloseEvent>
#include <QIODevice>
#include <QDataStream>
#include <QSettings>
#include <QPoint>
#include <QAction>
#include <QMenu>
MyPlayer::MyPlayer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyPlayer)
{
ui->setupUi(this);
player = new MyMediaPlayer(this);
playList = new QMediaPlaylist(this);
player->setPlaylist(playList);
//init trayIcon action
showTheWindow = new QAction(QString("show window"), this);
exitProgram = new QAction(QString("exit"), this);
trayMenu = new QMenu(this);
trayMenu->addAction(showTheWindow);
trayMenu->addAction(exitProgram);
QIcon icon = QIcon(":/icon/coverIcon.png");
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(icon);
trayIcon->setToolTip(QString("Music Player"));
trayIcon->setContextMenu(trayMenu);
/************just test*************
playList->addMedia(QUrl::fromLocalFile("E:/Users/jiang/Desktop/testmusic/All Along the Watchtower.mp3"));
playList->addMedia(QUrl::fromLocalFile("E:/Users/jiang/Desktop/testmusic/Crazy Train.mp3"));
***********************************/
//basic data
QObject::connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(getDuration(qint64)));
//control volume
QObject::connect(ui->volumeHorizontalSlider, SIGNAL(valueChanged(int)), player, SLOT(setVolume(int)));
QObject::connect(player, SIGNAL(volumeChanged(int)), ui->volumeHorizontalSlider, SLOT(setValue(int)));
QObject::connect(player, SIGNAL(volumeChanged(int)), this, SLOT(setVolumeLabelValue(int)));
QObject::connect(player, SIGNAL(volumeChanged(int)), this, SLOT(getVolume(int)));
//control position
QObject::connect(ui->positionHorizontalSlider, SIGNAL(sliderMoved(int)), player, SLOT(setPlayerPositionValue(int)));
QObject::connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(setSliderPostionValue(qint64)));
QObject::connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(setPositionLabelValue(qint64)));
//control state
QObject::connect(ui->stopButton, SIGNAL(clicked(bool)), player, SLOT(setPlayerState()));
//next and previous
QObject::connect(ui->previousButton, SIGNAL(clicked(bool)), playList, SLOT(previous()));
QObject::connect(ui->nextButton, SIGNAL(clicked(bool)), playList, SLOT(next()));
//select music from fileList
QObject::connect(ui->filesListWidget, SIGNAL(currentRowChanged(int)), playList, SLOT(setCurrentIndex(int)));
//add directory
QObject::connect(ui->addDirButton, SIGNAL(clicked(bool)), this, SLOT(addDir()));
QObject::connect(ui->refreshButton, SIGNAL(clicked(bool)), this, SLOT(searchLocalFiles()));
//set playbackMode
QObject::connect(playList, SIGNAL(playbackModeChanged(QMediaPlaylist::PlaybackMode)), this, SLOT(getPlaybackMode(QMediaPlaylist::PlaybackMode)));
QObject::connect(ui->playbackModeButton, SIGNAL(clicked(bool)), this, SLOT(setPlaybackMode()));
//control mainWindow by titleBar
QObject::connect(ui->titleBar, SIGNAL(dragWindow(QPoint)), this, SLOT(moveWindow(QPoint)));
QObject::connect(ui->titleBar, SIGNAL(windowStateChanged(MyTitleBar::WindowState)), this, SLOT(setMainWindowState(MyTitleBar::WindowState)));
//flash info widget
QObject::connect(player, SIGNAL(flashMetaData(MyMetaData)), ui->infoWidget, SLOT(flashWidgetInfo(MyMetaData)));
//flash lrc widget
QObject::connect(player, SIGNAL(expectLyricsPath(QString)), ui->LRCWidget, SLOT(tryToLoadLRCFile(QString)));
QObject::connect(player, SIGNAL(positionChanged(qint64)), ui->LRCWidget, SLOT(flashCurrentPosition(qint64)));
//tray icon
QObject::connect(showTheWindow, SIGNAL(triggered(bool)), this, SLOT(show()));
QObject::connect(exitProgram, SIGNAL(triggered(bool)), this, SLOT(close()));
QObject::connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(dealWithTrayIconActivated(QSystemTrayIcon::ActivationReason)));
/************just test**************
player->setVolume(50);
player->play();
***********************************/
init();
}
MyPlayer::~MyPlayer()
{
delete ui;
}
void MyPlayer::getDuration(qint64 dur)
{
duration = dur;
qDebug() << "duration:" << duration;
//set range of silder
ui->positionHorizontalSlider->setMinimum(0);
ui->positionHorizontalSlider->setMaximum(duration);
//show duration on label
ui->durationLabel->setText(QTime::fromMSecsSinceStartOfDay(static_cast<int>(duration)).toString("hh:mm:ss"));
}
void MyPlayer::setVolumeLabelValue(int vol)
{
ui->volumeLabel->setText(QString::number(vol));
}
void MyPlayer::getVolume(int vol)
{
volume = vol;
}
void MyPlayer::setSliderPostionValue(qint64 pos)
{
//qDebug() << "player pos:" << pos;
ui->positionHorizontalSlider->setValue(static_cast<int>(pos));
}
void MyPlayer::setPositionLabelValue(qint64 pos)
{
ui->positionLabel->setText(QTime::fromMSecsSinceStartOfDay(static_cast<int>(pos)).toString("hh:mm:ss"));
}
void MyPlayer::loadMedia()
{
//creat a messagebox if on dir in dirList
if (ui->dirListWidget->count() == 0) {
QMessageBox msgBox;
msgBox.setWindowIcon(QIcon(":/icon/dirMessageBoxIcon.png"));
msgBox.setWindowTitle("Add Dir");
msgBox.setIconPixmap(QPixmap(":/icon/addDir.png"));
msgBox.setTextFormat(Qt::RichText);
msgBox.setText(QObject::tr("<b>Add a music directory<b/>"));
msgBox.setInformativeText(QObject::tr("Do you want add a directory?"));
msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
if (ret == QMessageBox::Ok)
addDir();
}
else
searchLocalFiles();
}
void MyPlayer::addDir()
{
//get a dir path by a QFileDialog and research all dir
QString dir = QFileDialog::getExistingDirectory(this, QObject::tr("Select Directory"), "/home",
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (dir != "") {
//avoid add same dir
for (int i = 0; i < ui->dirListWidget->count(); i++)
if (dir == ui->dirListWidget->item(i)->text())
return;
//add new dir to dirList
ui->dirListWidget->addItem(dir);
//search files from dirList
searchLocalFiles();
}
}
void MyPlayer::searchLocalFiles()
{
//clear all from playList and filesList
playList->clear();
int i;
int k;
QStringList filesList;
//ui->dirListWidget->addItem("E:/Users/jiang/Desktop/testmusic");
for (i = 0; i < ui->dirListWidget->count(); i++) {
QString dirName(ui->dirListWidget->item(i)->text());
QFileInfoList filesListInfo;
qDebug() << dirName;
QDir dir(dirName);
QStringList filters;
filters << "*.mp3";
dir.setNameFilters(filters);
filesListInfo = dir.entryInfoList();
for (int j = 0; j < filesListInfo.size(); j++)
filesList << filesListInfo.at(j).filePath();
}
/************just test*************
QStringList fileslist;
filesList << "E:/Users/jiang/Desktop/testmusic/All Along the Watchtower.mp3"
<< "E:/Users/jiang/Desktop/testmusic/Crazy Train.mp3";
***********************************/
for (k = 0; k < filesList.size(); k++)
playList->addMedia(QUrl::fromLocalFile(filesList.at(k)));
//flash filesListWidget
ui->filesListWidget->clear();
for (int l = 0; l < playList->mediaCount(); l++) {
QListWidgetItem *fileItem = new QListWidgetItem(ui->filesListWidget);
QFile file(playList->media(l).canonicalUrl().path());
fileItem->setText(QFileInfo(file).baseName());
ui->filesListWidget->addItem(fileItem);
}
}
void MyPlayer::getPlaybackMode(QMediaPlaylist::PlaybackMode mode)
{
playbackMode = mode;
//debug information
switch (mode) {
case QMediaPlaylist::CurrentItemOnce:
qDebug() << "current item once";
break;
case QMediaPlaylist::CurrentItemInLoop:
qDebug() << "current item in loop";
break;
case QMediaPlaylist::Sequential:
qDebug() << "current item sequential";
break;
case QMediaPlaylist::Loop:
qDebug() << "current item loop";
break;
case QMediaPlaylist::Random:
qDebug() << "current item random";
break;
default:
qDebug() << "error playback mode";
break;
}
}
void MyPlayer::setPlaybackMode()
{
switch (playbackMode) {
case QMediaPlaylist::CurrentItemOnce:
playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
ui->playbackModeButton->setIcon(QIcon(":/icon/CurrentItemOnce.png"));
//change icon and other
break;
case QMediaPlaylist::CurrentItemInLoop:
playList->setPlaybackMode(QMediaPlaylist::Sequential);
break;
case QMediaPlaylist::Sequential:
playList->setPlaybackMode(QMediaPlaylist::Loop);
break;
case QMediaPlaylist::Loop:
playList->setPlaybackMode(QMediaPlaylist::Random);
break;
case QMediaPlaylist::Random:
playList->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
break;
default:
qDebug() << "set playback mode error";
break;
}
}
void MyPlayer::closeEvent(QCloseEvent *event)
{
qDebug() << "close";
saveCurrentInfo();
event->accept();
}
void MyPlayer::saveCurrentInfo()
{
qDebug() << "save";
//save dirList
int dirListCount = ui->dirListWidget->count();
QFile DirListData("DirList.dat");
DirListData.open(QIODevice::WriteOnly);
QDataStream out(&DirListData);
out << dirListCount;
for (int i = 0; i < dirListCount; i++) {
out << ui->dirListWidget->item(i)->text();
}
DirListData.close();
//save setting
QSettings settings("sets.ini", QSettings::IniFormat);
settings.setValue("volume", volume);
settings.setValue("playbackMode", playbackMode);
}
void MyPlayer::init()
{
int dirListCount;
QFile DirListData("DirList.dat");
DirListData.open(QIODevice::ReadOnly);
QDataStream in(&DirListData);
in >> dirListCount;
//qDebug() << "init list count:" << dirListCount;
for(int i = 0; i < dirListCount; i++) {
QString text;
in >> text;
qDebug() << text;
ui->dirListWidget->addItem(text);
}
//read setting
QSettings settings("sets.ini", QSettings::IniFormat);
player->setVolume(settings.value("volume").toInt());
playbackMode = static_cast<QMediaPlaylist::PlaybackMode>(settings.value("playbackmode").toInt());
this->setPlaybackMode();
loadMedia();
}
void MyPlayer::moveWindow(const QPoint &point)
{
this->move(this->pos() + point);
}
void MyPlayer::setMainWindowState(MyTitleBar::WindowState state)
{
switch (state) {
case MyTitleBar::Tray:
this->hideWindow();
break;
case MyTitleBar::Minimize:
this->setWindowState(Qt::WindowMinimized);
break;
case MyTitleBar::Close:
this->closeWindow();
break;
default:
qDebug() << "set 'Main Window State' error";
}
}
void MyPlayer::closeWindow()
{
this->close();
}
void MyPlayer::hideWindow()
{
this->hide();
trayIcon->show();
}
void MyPlayer::dealWithTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger)
this->show();
}