-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.cpp
More file actions
124 lines (111 loc) · 3.83 KB
/
Copy pathplaylist.cpp
File metadata and controls
124 lines (111 loc) · 3.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
#include "playlist.h"
Playlist::Playlist(const Playlist &playlist) {}
const uint Playlist::addSongToList(const Playlist::Song &song) {
auto pos = findSongByPath(song.path);
if (pos == UINT16_MAX) {
play_list.emplace_back(song);
return play_list.size() - 1;
}
return UINT16_MAX;
}
void Playlist::removeSongByIndex(const uint index) {
if (index >= play_list.size() - 1)
play_list.erase(play_list.begin() + index);
}
void Playlist::removeSongByPath(const QString &path) {
uint idx = 0;
for (auto& song : play_list) {
if (!song.path.compare(path)) {
play_list.removeAt(idx);
break;
}
idx++;
}
}
void Playlist::clearPlaylist() {
play_list.clear();
}
const uint Playlist::findSongByPath(const QString &path) {
uint idx = 0;
for (auto& song: play_list) {
if (!song.path.compare(path)) {
return idx;
}
idx += 1;
}
return UINT16_MAX; // 特殊值:找不到路径时输出
}
const uint Playlist::getSongCount() const {
return play_list.size();
}
const QList<Playlist::Song> Playlist::getPlayList() const {
return play_list;
}
const Playlist::Song Playlist::getSongByIndex(const uint index) {
if (index >= play_list.size()) return Song();
return play_list.at(index);
}
const Playlist::Song Playlist::getSongByPath(const QString &path) {
for (auto& song : play_list) {
if (!song.path.compare(path)) {
return song;
}
}
return Song();
}
bool Playlist::parseSongFromFileName(const QString &path, Song &song) {
QFileInfo file(path);
// 判断是否是文件
if (!file.isFile()) {
qDebug() << "[Debug] File is not valid!";
return false;
}
QByteArray codec = file.absoluteFilePath().toLocal8Bit();
// 解析音乐文件
qDebug() << "[Debug] Parsed" << codec.toStdString().c_str();
TagLib::FileRef fileRef(codec.toStdString().c_str());
// 文件无法解析!
if (!fileRef.file()) return false;
TagLib::Tag *music_tag = fileRef.tag(); // 获取音乐文件下的标签
Song temp_song; // 用于将解析结果存入此结构体中
temp_song.path = path;
temp_song.title = file.fileName(); // 暂时以文件名命名标题
// 若无法解析标签
if (!music_tag) {
qInfo() << "[Error] Can not parse the file" << path << "!";
temp_song.album = "未知专辑";
temp_song.artist = "未知歌手";
play_list.emplace_back(temp_song);
song = temp_song;
return true;
}
QString m_title = music_tag->title().toCString(true);
// 若此音乐文件有标题
if (!m_title.isEmpty()) {
temp_song.title = m_title;
QString artist = music_tag->artist().toCString(true);
QString album = music_tag->album().toCString(true);
temp_song.artist = (artist.simplified().isEmpty() ? "未知歌手" : artist);
temp_song.album = (album.simplified().isEmpty() ? "未知专辑" : album);
} else {
temp_song.album = "未知专辑";
temp_song.artist = "未知歌手";
}
// 已成功解析
play_list.emplace_back(temp_song);
song = temp_song;
return true;
}
bool Playlist::swap(const uint i1, const uint i2) {
if (i1 > play_list.size() - 1 || i2 > play_list.size() - 1) return false;
Song temp_song = {play_list[i1].title, play_list[i1].artist, play_list[i1].album, play_list[i1].path};
play_list[i1].title = play_list[i2].title;
play_list[i1].artist = play_list[i2].artist;
play_list[i1].album = play_list[i2].album;
play_list[i1].path = play_list[i2].path;
play_list[i2].title = temp_song.title;
play_list[i2].artist = temp_song.artist;
play_list[i2].album = temp_song.album;
play_list[i2].path = temp_song.path;
return true;
}