-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.cpp
116 lines (106 loc) · 3.64 KB
/
player.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
#include "player.h"
#include "playlistmodel.h"
#include <QMediaService>
#include <QMediaPlaylist>
#include <QMediaMetaData>
#include <QObject>
#include <QFileInfo>
#include <QTime>
#include <QDir>
#include <QStandardPaths>
Player::Player(QObject *parent)
: QObject(parent)
{
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(this);
m_player->setPlaylist(m_playlist);
m_playlistModel = new PlaylistModel(this);
open();
if (!m_playlist->isEmpty()) {
m_playlist->setCurrentIndex(0);
}
}
void Player::open()
{
QDir directory(QStandardPaths::standardLocations(QStandardPaths::MusicLocation)[0]);
QFileInfoList musics = directory.entryInfoList(QStringList() << "*.mp3",QDir::Files);
QList<QUrl> urls;
for (int i = 0; i < musics.length(); i++){
urls.append(QUrl::fromLocalFile(musics[i].absoluteFilePath()));
}
addToPlaylist(urls);
}
void Player::addToPlaylist(const QList<QUrl> &urls)
{
for (auto &url: urls) {
m_playlist->addMedia(url);
FileRef f(url.path().toStdString().c_str());
Tag *tag = f.tag();
Song song(QString::fromWCharArray(tag->title().toCWString()),
QString::fromWCharArray(tag->artist().toCWString()),url.toDisplayString(),
getAlbumArt(url));
m_playlistModel->addSong(song);
}
}
QString Player::getTimeInfo(qint64 currentInfo)
{
QString tStr = "00:00";
currentInfo = currentInfo/1000;
qint64 durarion = m_player->duration()/1000;
if (currentInfo || durarion) {
QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60,
currentInfo % 60, (currentInfo * 1000) % 1000);
QTime totalTime((durarion / 3600) % 60, (m_player->duration() / 60) % 60,
durarion % 60, (m_player->duration() * 1000) % 1000);
QString format = "mm:ss";
if (durarion > 3600)
format = "hh::mm:ss";
tStr = currentTime.toString(format);
}
return tStr;
}
QString Player::getAlbumArt(QUrl url)
{
static const char *IdPicture = "APIC" ;
TagLib::MPEG::File mpegFile(url.path().toStdString().c_str());
TagLib::ID3v2::Tag *id3v2tag = mpegFile.ID3v2Tag();
TagLib::ID3v2::FrameList Frame ;
TagLib::ID3v2::AttachedPictureFrame *PicFrame ;
void *SrcImage ;
unsigned long Size ;
FILE *jpegFile;
jpegFile = fopen(QString(url.fileName()+".jpg").toStdString().c_str(),"wb");
if ( id3v2tag )
{
// picture frame
Frame = id3v2tag->frameListMap()[IdPicture] ;
if (!Frame.isEmpty() )
{
for(TagLib::ID3v2::FrameList::ConstIterator it = Frame.begin(); it != Frame.end(); ++it)
{
PicFrame = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(*it) ;
// if ( PicFrame->type() ==
//TagLib::ID3v2::AttachedPictureFrame::FrontCover)
{
// extract image (in it’s compressed form)
Size = PicFrame->picture().size() ;
SrcImage = malloc ( Size ) ;
if ( SrcImage )
{
memcpy ( SrcImage, PicFrame->picture().data(), Size ) ;
fwrite(SrcImage,Size,1, jpegFile);
fclose(jpegFile);
free( SrcImage ) ;
return QUrl::fromLocalFile(url.fileName()+".jpg").toDisplayString();
}
}
}
}
}
else
{
qDebug() <<"id3v2 not present";
return "qrc:/Image/album_art.png";
}
return "qrc:/Image/album_art.png";
}