-
Notifications
You must be signed in to change notification settings - Fork 2
/
playermainwindow.cpp
413 lines (370 loc) · 13.6 KB
/
playermainwindow.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
406
407
408
409
410
411
412
413
#include <QFileDialog>
#include <QInputDialog>
#include <QStringList>
#include <QTextCodec>
#include <QPixmap>
#include <QDebug>
#include <QtCore>
#include <QObject>
#include <QCloseEvent>
#include <QMessageBox>
#include "playermainwindow.h"
#include "ui_playermainwindow.h"
#include "network.h"
#define KMusicChannelsURL "http://raw.github.com/iosnews/test/master/baidu.json"
#define kMusicPlaylistURL "http://fm.baidu.com/dev/api/?tn=playlist&special=flash&prepend=&format=json&&id="
#define kMusicURL "http://music.baidu.com/data/music/fmlink?type=mp3&rate=2&format=json&songIds="
#define kMusicLRCURL "http://fm.baidu.com/"
PlayerMainWindow::PlayerMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::PlayerMainWindow)
{
ui->setupUi(this);
this->osdLyricsWidget = new OSDLyricsWidget();
this->osdLyricsWidget->show();
this->localMediaPlayList = new QMediaPlaylist(this);
this->onlineMediaPlayList = new QMediaPlaylist(this);
this->onlineMediaList = new QList<KMediaInfo>();
this->mediaPlayer = new QMediaPlayer(this);
this->mediaPlayer->setPlaylist(this->localMediaPlayList);
this->mediaPlayer->setVolume(50);
this->progressTimer = new QTimer(this);
this->progressTimer->setInterval(100);
this->network = new Network(this);
network->fetchChannels(KMusicChannelsURL);
this->setupConnections();
this->dbOK = db.initDataBase();
}
PlayerMainWindow::~PlayerMainWindow()
{
delete ui;
delete localMediaPlayList;
delete mediaPlayer;
delete progressTimer;
delete network;
delete onlineMediaList;
delete onlineMediaPlayList;
delete osdLyricsWidget;
}
void PlayerMainWindow::setupConnections()
{
connect(ui->localAction, SIGNAL(triggered()), this, SLOT(openLocalMedia()));
connect(ui->netAction, SIGNAL(triggered()), this, SLOT(openInternetURL()));
connect(ui->playButton, SIGNAL(clicked()), this, SLOT(playButtonClicked()));
connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
connect(ui->playTimeSlider, SIGNAL(sliderMoved(int)), this, SLOT(playSliderValueChanged(int)));
connect(ui->volumeDial, SIGNAL(valueChanged(int)), this, SLOT(volumeDialValueChanged(int)));
connect(ui->channelComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(channelIndexChanged(int)));
connect(this->mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(playerStateChanged(QMediaPlayer::State)));
connect(this->mediaPlayer, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)));
connect(this->progressTimer, SIGNAL(timeout()), this, SLOT(playProgressUpdate()));
connect(this->mediaPlayer, SIGNAL(metaDataChanged()), this, SLOT(metaDataUpdate()));
connect(this->ui->aboutAction, SIGNAL(triggered()), this,SLOT(showAbout()));
connect(this,SIGNAL(destroyed()), this->osdLyricsWidget, SLOT(close()));
//networks signals
connect(this->network, SIGNAL(musicChannelsFetched(QList<KChannel>*)), this, SLOT(channelsDownloaded(QList<KChannel>*)));
connect(this->network, SIGNAL(mediaInfoListFetched(QList<KMediaInfo>*)), this, SLOT(mediaInfoListDownloaded(QList<KMediaInfo>*)));
connect(this->network, SIGNAL(mediaInfoFetched(KMediaInfo)), this, SLOT(mediaInfoDownloaded(KMediaInfo)));
connect(this->network, SIGNAL(imageDataFetched(QByteArray*)), this,SLOT(AlbumImageDownloaded(QByteArray*)));
connect(this->network, SIGNAL(lyricsDataFetched(QByteArray*)), this->osdLyricsWidget, SLOT(handleNewLyricsData(QByteArray*)));
//favorites
connect(this->ui->addFavAction, SIGNAL(triggered()), this, SLOT(addCurrentSongToFavorites()));
connect(this->ui->playFavAction, SIGNAL(triggered()), this, SLOT(playSongInFavorites()));
}
void PlayerMainWindow::closeEvent(QCloseEvent *event)
{
this->osdLyricsWidget->close();
event->accept();
}
void PlayerMainWindow::openLocalMedia()
{
QStringList fileNameList;
fileNameList = QFileDialog::getOpenFileNames(this,
tr("播放本地歌曲"), "", tr("MP3/MPEG4音频 (*.mp3 *.m4a);;"));
if (!fileNameList.isEmpty())
{
this->localMediaPlayList->clear();
foreach (const QString &fileName, fileNameList) {
QMediaContent media = QMediaContent(QUrl::fromLocalFile(fileName));
this->localMediaPlayList->addMedia(media);
}
this->localMediaPlayList->setCurrentIndex(0);
}
else
{
//取消
}
qDebug() << fileNameList;
return ;
}
void PlayerMainWindow::openInternetURL()
{
QString urlString;
bool ok;
urlString = QInputDialog::getText(this,
tr("播放网络歌曲"),tr("请输入歌曲网址(尚未实现)"),QLineEdit::Normal,"http://", &ok);
if (ok && !urlString.isEmpty())
{
//inputed
}
qDebug() << urlString;
return;
}
void PlayerMainWindow::playButtonClicked()
{
if(this->mediaPlayer->state() == QMediaPlayer::PlayingState)
{
this->mediaPlayer->pause();
qDebug() << "pause";
}else
{
this->mediaPlayer->setVolume(this->ui->volumeDial->value());
this->mediaPlayer->play();
qDebug() << "play";
}
}
void PlayerMainWindow::prevButtonClicked()
{
qDebug() << "prev";
QMediaPlaylist *currentPlayList = this->onlineMode?this->onlineMediaPlayList:this->onlineMediaPlayList;
currentPlayList->previous();
this->osdLyricsWidget->handleNewLyricsData(NULL);
}
void PlayerMainWindow::nextButtonClicked()
{
qDebug() << "next";
QMediaPlaylist *currentPlayList = this->onlineMode?this->onlineMediaPlayList:this->onlineMediaPlayList;
currentPlayList->next();
this->osdLyricsWidget->handleNewLyricsData(NULL);
}
void PlayerMainWindow::playSliderValueChanged(int value)
{
qDebug() << "slider changed " << value;
float percent = (value * 1.0) / this->ui->playTimeSlider->maximum();
int64_t pos = this->mediaPlayer->duration() * percent;
this->mediaPlayer->setPosition(pos);
}
void PlayerMainWindow::volumeDialValueChanged(int value)
{
qDebug() << "Dial changed " << value;
this->mediaPlayer->setVolume(value);
}
void PlayerMainWindow::channelIndexChanged(int index)
{
qDebug() << "combox changed " << index;
if(index < 0)
{
return;
}else if(index == 0)
{
//local media
this->onlineMode = false;
this->mediaPlayer->setPlaylist(this->localMediaPlayList);
return;
}else if(index == 1)
{
//update internet media channel
network->fetchChannels(KMusicChannelsURL);
this->onlineMode = true;
this->onlineMediaPlayList->clear();
this->mediaPlayer->setPlaylist(this->onlineMediaPlayList);
return;
}
this->onlineMode = true;
this->onlineMediaPlayList->clear();
this->mediaPlayer->setPlaylist(this->onlineMediaPlayList);
QComboBox *channelComboBox = (QComboBox*)sender();
QString channelID = channelComboBox->itemData(index).toString();
this->network->fetchMediaInfoList(kMusicPlaylistURL + channelID);
}
void PlayerMainWindow::playerStateChanged(QMediaPlayer::State state)
{
if(QMediaPlayer::PlayingState == state)
{
// this->ui->playButton->setText(tr("暂停"));
this->ui->playButton->setIcon(QIcon(":/images/pause.png"));
this->progressTimer->start();
this->osdLyricsWidget->pauseTimer(false);
}else
{
// this->ui->playButton->setText(tr("播放"));
this->ui->playButton->setIcon(QIcon(":/images/play.png"));
this->progressTimer->stop();
this->osdLyricsWidget->pauseTimer(true);
}
qDebug() << "state changed " << state;
}
void PlayerMainWindow::mediaStatusChanged(QMediaPlayer::MediaStatus status)
{
switch(status)
{
case QMediaPlayer::NoMedia:
case QMediaPlayer::EndOfMedia:
this->mediaPlayer->playlist()->next();
break;
default:
;
}
qDebug() << "status changed " << status;
}
void PlayerMainWindow::playProgressUpdate()
{
int64_t pos = this->mediaPlayer->position();
int64_t duration = this->mediaPlayer->duration();
int value = 100 * (1.0*pos)/duration;
this->ui->playTimeSlider->setValue(value);
this->osdLyricsWidget->updateLyrics(pos);
}
void PlayerMainWindow::metaDataUpdate()
{
QString title, subTitle, albumTitle, albumArtist;
QImage coverArtImage;
QPixmap pixmap;
if(!this->onlineMode)
{
title = this->mediaPlayer->metaData("Title").toString();
subTitle = this->mediaPlayer->metaData("SubTitle").toString();
albumTitle = this->mediaPlayer->metaData("AlbumTitle").toString();
albumArtist = this->mediaPlayer->metaData("AlbumArtist").toString();
coverArtImage = this->mediaPlayer->metaData("CoverArtImage").value<QImage>();
if(coverArtImage.isNull())
{
pixmap = QPixmap(":/images/Qt.png");
}else
{
pixmap.convertFromImage(coverArtImage);
}
}else
{
int curIndex = this->onlineMediaPlayList->currentIndex();
if(curIndex < 0 || curIndex > this->onlineMediaList->count() - 1)
{
return;
}
KMediaInfo curInfo = this->onlineMediaList->at(curIndex);
title = curInfo.songName;
albumTitle = curInfo.albumName;
albumArtist = curInfo.artistName;
this->network->fetchImage(curInfo.songPicUrl);
this->network->fetchLyrics(kMusicLRCURL + curInfo.lrcUrl);
}
this->ui->singerLabel->setText("歌手: "+albumArtist);
this->ui->albumLabel->setText("专辑: "+albumTitle);
this->ui->songLabel->setText("歌名: "+title);
if(!pixmap.isNull())
{
this->ui->artWorkLabel->setPixmap(pixmap.scaled(this->ui->artWorkLabel->size()));
}
}
void PlayerMainWindow::playOnlineMedia(int index)
{
this->mediaPlayer->playlist()->setCurrentIndex(index);
this->mediaPlayer->play();
qDebug()<<this->mediaPlayer->errorString();
}
void PlayerMainWindow::addCurrentSongToFavorites()
{
if(this->onlineMode)
{
int index=this->mediaPlayer->playlist()->currentIndex();
if(index<0||index>=this->onlineMediaList->count())
{
return;
}
this->db.addFavoriteSong(this->onlineMediaList->at(index));
}
}
void PlayerMainWindow::playSongInFavorites()
{
QList<KMediaInfo> * mediaInfoList = this->db.getFavoriteSong();
if(mediaInfoList && mediaInfoList->count()>0)
{
this->onlineMediaPlayList->clear();
this->onlineMediaList->clear();
this->mediaPlayer->setPlaylist(this->onlineMediaPlayList);
this->onlineMode = true;
}
for(int i = 0; mediaInfoList&&i<mediaInfoList->count();i++)
{
KMediaInfo mediaInfo = mediaInfoList->at(i);
QUrl url = QUrl(mediaInfo.mp3Url);
QMediaContent content(url);
this->onlineMediaPlayList->addMedia(content);
this->onlineMediaList->append(mediaInfo);
if(this->onlineMediaPlayList->mediaCount() == 1)
{
this->playOnlineMedia(0);
}
}
}
void PlayerMainWindow::showAbout()
{
QMessageBox::about(this, tr("关于 ") + qApp->applicationName(),
tr("<h2>UbuntuKylin简易网络播放器</h2><br/>"
" KylinPlayer 是<a href=\"http://www.ubuntukylin.com\">优麒麟</a>为"
"湖南科技职业学院 <br/>2014年国培班学员学习Qt5开发的简易音乐播放器.<br/>"
"支持: Ubuntu Kylin/Mac OS X/Windows.<br/><br/>"
"作者: \t大茶园丁(戴维营教育) <br/>"
"邮箱: \t<a href=\"mailto:[email protected]?subject=Feedback for KyinPlayer&body=Ubuntu KylinPlayer\">[email protected]</a>.<br/>"
"网站: \t<a href=\"http://www.ubuntukylin.com\">http://www.ubuntukylin.com.</a><br/>"
"反馈: \t <a href=\"http://www.ubuntukylin.com/ukylin/\">http://www.ubuntukylin.com/ukylin/</a><br/><br/>"
"<img src=\":/images/ubuntukylin.png\" ></img>"));
}
void PlayerMainWindow::channelsDownloaded(QList<KChannel> *channelList)
{
if (channelList->count() <= 0)
{
return ;
}
this->ui->channelComboBox->clear();
this->ui->channelComboBox->addItems(QStringList()<<"本地歌曲"<<"网络歌曲");
for(int i = 0; i < channelList->count(); i++)
{
QString id = channelList->value(i).id;
QString title = channelList->value(i).title;
this->ui->channelComboBox->addItem(title, QVariant(id));
}
}
void PlayerMainWindow::mediaInfoListDownloaded(QList<KMediaInfo> *mediaInfoList)
{
if(mediaInfoList->count() <= 0)
{
return;
}
int count = mediaInfoList->count();
this->onlineMediaList->clear();
this->onlineMediaList->reserve(count);
for(int i = 0; i < mediaInfoList->count(); i++)
{
KMediaInfo mediaInfo = mediaInfoList->at(i);
this->network->fetchSongInfo(kMusicURL+ QString("%1").arg(mediaInfo.id));
}
qDebug() << mediaInfoList;
}
void PlayerMainWindow::mediaInfoDownloaded(KMediaInfo mediaInfo)
{
QUrl url = QUrl(mediaInfo.mp3Url);
QMediaContent content(url);
this->onlineMediaPlayList->addMedia(content);
this->onlineMediaList->append(mediaInfo);
if(this->onlineMediaPlayList->mediaCount() == 1)
{
this->playOnlineMedia(0);
}
return;
}
void PlayerMainWindow::AlbumImageDownloaded(QByteArray *imageData)
{
QImage img;
QPixmap pixmap;
if(imageData->isEmpty())
{
pixmap = QPixmap(":/images/Qt.png");
}else
{
img.loadFromData(*imageData);
pixmap.convertFromImage(img);
}
this->ui->artWorkLabel->setPixmap(pixmap.scaled(this->ui->artWorkLabel->size()));
}