-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsong.cpp
More file actions
206 lines (179 loc) · 4.83 KB
/
Copy pathsong.cpp
File metadata and controls
206 lines (179 loc) · 4.83 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
#include "song.h"
Song::Song()
:_like(false),
_history(false)
{}
Song::Song(const QUrl &url)
:_like(false),
_history(false),
_musicurl(url)
{
//通过uuid来确保id的唯一性
_musicid=QUuid::createUuid().toString();
ParseMediaData();
}
void Song::IsLike(bool flag)
{
_like=flag;
}
void Song::IsHistory(bool flag)
{
_history=flag;
}
void Song::SetMusicName(const QString &name)
{
_musicname=name;
}
void Song::SetSinger(const QString &singer)
{
_singer=singer;
}
void Song::SetAblum(const QString &ablum)
{
_album=ablum;
}
void Song::SetDuration(const qint64 &sec)
{
_duration=sec;
}
void Song::SetMusicId(const QString &id)
{
_musicid=id;
}
void Song::SetMusicUrl(const QUrl &url)
{
_musicurl=url;
}
bool Song::Like()
{
return _like;
}
bool Song::History()
{
return _history;
}
QString Song::GetMusicName()
{
return _musicname;
}
QString Song::GetSinger()
{
return _singer;
}
QString Song::GetAblum()
{
return _album;
}
qint64 Song::GetDuration()
{
return _duration;
}
QString Song::GetMusicId()
{
return _musicid;
}
QUrl Song::GetMusicUlr()
{
return _musicurl;
}
QString Song::GetLrcFile()
{
QString path=_musicurl.toLocalFile();
path.replace(".mp3", ".lrc");
path.replace(".wav", ".lrc");
path.replace(".flac", ".lrc");
path.replace(".mpga", ".lrc");
path.replace(".m4a", ".lrc");
return path;
}
void Song::insertMuiscToDB()
{
QSqlQuery query;
query.prepare("select exists (select 1 from musicInfo where musicid=?)");
query.addBindValue(_musicid);
if(query.exec()==false)
{
qDebug()<<"select false"<<query.lastError().text();
return;
}
if(query.next())
{
bool isexists=query.value(0).toBool();
if(isexists)
{
// 修正参数顺序:set 子句3个?,where 子句1个?
query.prepare("update musicInfo set islike=?,ishistory=?,musicname=? where musicid=?");
query.addBindValue(_like?1:0);
query.addBindValue(_history?1:0);
query.addBindValue(_musicname); // 对应第3个? (musicname)
query.addBindValue(_musicid); // 对应 where 条件的 ? (musicid)
if(query.exec()==false)
{
qDebug()<<"update false";
}
qDebug()<<"update success"<<_musicname<<_musicid;
}else
{
query.prepare("insert into musicInfo(musicid,musicname,singer,album,musicurl,duration,islike,ishistory)values(?,?,?,?,?,?,?,?)");
query.addBindValue(_musicid);
query.addBindValue(_musicname);
query.addBindValue(_singer);
query.addBindValue(_album);
query.addBindValue(_musicurl.toLocalFile());
query.addBindValue(_duration);
query.addBindValue(_like?1:0);
query.addBindValue(_history?1:0);
if(query.exec()==false)
{
qDebug()<<"insert false";
}
qDebug()<<"insert success"<<_musicname<<_musicid;
}
}
}
void Song::ParseMediaData()
{
QMediaPlayer player;
QEventLoop loop;
QObject::connect(&player,&QMediaPlayer::mediaStatusChanged,&loop,&QEventLoop::quit);
QObject::connect(&player,&QMediaPlayer::errorOccurred,&loop,[&loop](QMediaPlayer::Error,const QString&errorString){
qDebug()<<"解析失败"<<errorString;
loop.quit();
});
player.setSource(_musicurl);
QTimer::singleShot(2000,&loop,&QEventLoop::quit);
loop.exec();
_musicname=player.metaData().value(QMediaMetaData::Title).toString();
_singer=player.metaData().value(QMediaMetaData::Author).toString();
_album=player.metaData().value(QMediaMetaData::AlbumTitle).toString();
_duration=player.duration();
// 直接用 QUrl::fileName() 避免 Windows 本地路径的 GBK 编码问题
QString filename = _musicurl.fileName();
// 去掉扩展名,方便后续截取
QString baseName = filename;
int dotIndex = baseName.lastIndexOf('.');
if (dotIndex != -1) {
baseName = baseName.mid(0, dotIndex);
}
// 尝试按 "歌手 - 歌名" 格式解析
int sepIndex = baseName.indexOf('-');
if (_musicname.isEmpty()) {
if (sepIndex != -1) {
_musicname = baseName.mid(0, sepIndex).trimmed();
} else {
_musicname = baseName; // 无法解析时才用完整文件名
}
}
if (_singer.isEmpty()) {
if (sepIndex != -1) {
_singer = baseName.mid(sepIndex + 1).trimmed();
} else {
_singer = "未知歌手";
}
}
if(_album.isEmpty())
{
_album="未知专辑";
}
qDebug()<<_musicname<<_singer<<_album;
}