-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmusic.php
More file actions
69 lines (60 loc) · 2.09 KB
/
music.php
File metadata and controls
69 lines (60 loc) · 2.09 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
<?php
// 设定模式:'local' 或 'remote'
$mode = 'local';
$songs = [];
if ($mode === 'local') {
// 模式 1: 读取本地目录下的音频文件(包括子文件夹)
$musicDir = './music';
function scanMusicDir($dir) {
$songs = [];
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
$fullPath = "$dir/$file";
if (is_dir($fullPath)) {
// 递归扫描子文件夹
$subSongs = scanMusicDir($fullPath);
$songs = array_merge($songs, $subSongs);
} elseif (preg_match('/\.(mp3|flac|wav|ogg)$/i', $file)) {
$fileName = pathinfo($file, PATHINFO_FILENAME);
list($artist, $title) = explode(' - ', $fileName, 2);
$songs[] = [
'url' => $fullPath,
'artist' => $artist,
'title' => $title
];
}
}
return $songs;
}
if (is_dir($musicDir)) {
$songs = scanMusicDir($musicDir);
} else {
http_response_code(404);
echo json_encode(['error' => 'Local music directory not found']);
exit;
}
} elseif ($mode === 'remote') {
// 模式 2: 读取远程直链地址
$remoteUrls = [
'https://localhost.com/卢冠廷 - 一生所爱.flac',
'https://localhost.com/吴雨霏 - 吴哥窟.flac',
'https://localhost.com/周杰伦 - 红尘客栈.mp3'
];
foreach ($remoteUrls as $url) {
$fileName = urldecode(pathinfo($url, PATHINFO_FILENAME));
list($artist, $title) = explode(' - ', $fileName, 2);
$songs[] = [
'url' => $url,
'artist' => $artist,
'title' => $title
];
}
} else {
http_response_code(500);
echo json_encode(['error' => 'Invalid mode configured in the script.']);
exit;
}
header('Content-Type: application/json');
echo json_encode($songs);
?>