-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.cpp
283 lines (249 loc) · 8.8 KB
/
music.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
#include "music.h"
#include <thread>
vector<vector<QString>> list_(1,vector<QString>(1));
QTextCodec* codec = QTextCodec::codecForName("GBK");
extern QQmlApplicationEngine* engine;
extern QQuickView* view;
int PLAYERTYPE = 0;
int MUSICPOS = 0;
int thisPOS = 0;
using namespace std;
using namespace this_thread;
Music::Music(QObject *p):
QObject(p)
{
now = new QMediaPlayer;
playlist = new QMediaPlaylist;
LrcList.clear();
timelist.clear();
nowtime = 0;
endtime = 0;
vol = 80;
tag = 0;
k = false;
}
Music::~Music()
{
delete now;
delete playlist;
delete myView;
LrcList.clear();
timelist.clear();
list_.clear();
}
void Music::ShowMusicList()
{
qmlRegisterType<Music>("my.Music",1,0,"Music");
_finddata_t file;
int k,i = 1;
long HANDLE;
k = HANDLE = _findfirst( "music/*.mp3", &file );
while( k != -1 ){
QString str = codec->toUnicode(file.name);
list_[0].push_back(str); //动态分配
list_[0][i]= str;
this->playlist->addMedia(QUrl("music/"+str));
k = _findnext( HANDLE, &file );
i++;
}
list_[0].push_back("");
list_[0][i] = "";
_findclose( HANDLE );
return;
}
void Music::startPlay(QString name)
{
if(this->now != NULL){
MUSICPOS = 0;
for(int i = 0; i<list_[0].size(); i++){ //遍历list_
if(list_[0][i] == name){ //找到用户点击的那个是list_中的第几个(就是传进来的name)
delete this->playlist; //删除之前的playlist 重new一个
this->playlist = new QMediaPlaylist;
for(int j = i; j<list_[0].size();j++){ //从i开始 也就是用户点击的那个音乐(name)开始 往后构建播放列表 前面的不要了
this->playlist->addMedia(QUrl("music/"+list_[0][j]));;
}
delete this->now; //删除当前的音乐指针 重new一个
this->now = new QMediaPlayer;
MUSICPOS =i;
this->playlist->setCurrentIndex(1); //这函数我也不知道干啥的 有人说是设置当前音乐为第一个播放?
this->now->setPlaylist(this->playlist); //对now指针 设置播放列表
cleraLrcView(); //先把当前的歌词表清了
QObject::connect(now,&QMediaPlayer::currentMediaChanged,[this](){
thisPOS= this->playlist->currentIndex();
cleraLrcView();
});
QObject::connect(now, &QMediaPlayer::positionChanged, [this](qint64 position){
if(this->now->duration() != 0)
this->setEndtime(this->now->duration()); //获取当前音乐的总时长
this->settime(position); //获得当前播放的位置(就是当前播放到哪了 单位:毫秒)
QQmlContext* s_time = this->myView->rootContext();
s_time->setContextProperty("mySTIME",QVariant(this->timeformat(position)));
QQmlContext* now_progress = this->myView->rootContext();
now_progress->setContextProperty("setNOW",QVariant(position));
QQmlContext* e_time = this->myView->rootContext();
e_time->setContextProperty("myETIME",QVariant(this->timeformat(this->endtime)));
QQmlContext* title = this->myView->rootContext();
//int index= this->playlist->currentIndex();
title->setContextProperty("myTITLE",QVariant(list_[0][thisPOS+MUSICPOS]));
showlrc(list_[0][thisPOS+MUSICPOS],position);
if(this->now->isAudioAvailable()){ //如果当前音乐可以播放
this->tag = MUSICPOS;
}
});
this->setVol(this->vol); //音量
this->now->play(); // Let's Play!
}
}
}
}
void Music::pausePlay(){
if(this->k == false && this->now != NULL){
this->now->pause();
k=true;
}
else if(this->k == true && this->now != NULL){
this->now->play();
k=false;
}
}
void Music::setVol(int v){
this->now->setVolume(v);
this->vol = v;
}
int Music::getVol(){return this->vol;}
void Music::lastMusic(){
if(list_[0][tag-1] != "" && this->now != NULL)
this->startPlay(list_[0][tag-1]);
}
void Music::nextMusic(){
if(list_[0][tag+1] != "" && this->now != NULL)
this->startPlay(list_[0][tag+1]);
}
QVariant Music::musicType(){
if(PLAYERTYPE > 4) PLAYERTYPE = 0;
switch (PLAYERTYPE) {
case 0:{
this->playlist->setPlaybackMode(QMediaPlaylist::Sequential);//顺序播放
QVariant t("///img/img/顺序播放.png");
PLAYERTYPE++;
return t;
}
case 1:{
this->playlist->setPlaybackMode(QMediaPlaylist::Loop);//列表循环
QVariant t("///img/img/列表循环.png");
PLAYERTYPE++;
return t;
}
case 2:{
this->playlist->setPlaybackMode(QMediaPlaylist::CurrentItemOnce); //当前只放一次
QVariant t("///img/img/单次播放.png");
PLAYERTYPE++;
return t;
}
case 3:{
this->playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);//当前循环
QVariant t("///img/img/当前循环.png");
PLAYERTYPE++;
return t;
}
case 4:{
this->playlist->setPlaybackMode(QMediaPlaylist::Random);//随机播放
QVariant t("///img/img/随机播放.png");
PLAYERTYPE++;
return t;
}
default:
QVariant t("");
return t;
}
}
QQuickView* Music::ViewMusicList()
{
QStringList dataList;
int a = 1;
while(list_[0][a] != ""){
dataList.append(list_[0][a]);
a++;
}
QQuickView* view;
QQmlContext *ctxt = view->rootContext();
ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
return view;
}
void Music::setNowMusicPos(qint64 time){
this->now->setPosition(time);
}
void Music::setview(QQmlApplicationEngine* v){
this->myView = v;
}
void Music::setEndtime(qint64 settime_){
this->endtime = settime_;
}
qint64 Music::getEndtime(){return this->endtime;}
qint64 Music::times(){
return this->nowtime;
}
qint64 Music::endTime(){
return this->endtime;
}
void Music::settime(qint64 time_){
this->nowtime = time_;
}
QString Music::timeformat(qint64 musictime){ //格式化时间 形参单位毫秒
int min = musictime/1000/60; //换算成分钟
int sec = musictime/1000; //换算成秒
while(sec>=60) sec = sec%60; //如果当前接受的时间大于等于60秒 除以60取其余数就是格式化的秒
QString outtime = QString::number(min) + ":" + QString::number(sec);
return outtime;
}
QString Music::getMusicTitle(){
return list_[0][this->tag];
}
QStringList Music::showlrc(QString name,qint64 time)
{
if(this->LrcList.isEmpty()){ //如果歌词表是空的
const QRegExp rx("\\[(\\d+):(\\d+(\\.\\d+)?)\\]"); //用来查找时间标签的正则表达式
QString name_ = name.section('.',0,0);
QFile mylrc("music/"+name_+".lrc");
if(!mylrc.open(QIODevice::ReadOnly | QIODevice::Text)) return this->LrcList;
while(!mylrc.atEnd()){
QString str = codec->toUnicode(mylrc.readLine());
QString lrc = str.section("]",1,1);
this->LrcList.append(lrc); //歌词列表
QQmlContext* lrc_list = this->myView->rootContext();
lrc_list->setContextProperty("myLRC",QVariant::fromValue(LrcList));
int pos =rx.indexIn(str);
if(pos > -1){
QString time = rx.cap(0);
QString t2 = time.mid(4,2) + time.mid(7,2);
qint64 totime = (time.mid(1,2).toInt()*60) + t2.toInt()/100;
this->timelist.push_back(totime);
}
}
}
else{
int timee = time/1000;
for(int i = 0; i < this->timelist.size(); i++){
if(timelist[i] == timee && timee!=0){
mysum = i*35; //设置滚动距离
positionChanged();
QQmlContext* y = this->myView->rootContext();
y->setContextProperty("LRC_Y",QVariant(mysum));//滚动
}
}
}
return this->LrcList;
}
void Music::clearLrc(){
this->LrcList.clear();
this->timelist.clear();
}
void Music::cleraLrcView(){
clearLrc();
QStringList* str = new QStringList;
str->append("没找到歌词");
QQmlContext* clear = this->myView->rootContext();
clear->setContextProperty("myLRC",QVariant::fromValue(*str));
delete str;
}