上传文件至 /

This commit is contained in:
2025-09-24 14:15:23 +00:00
parent 45af503e01
commit c0f38b5d71
5 changed files with 3113 additions and 0 deletions

144
gg.php Normal file
View File

@@ -0,0 +1,144 @@
<?php
// gg.php - 公告管理页面
// 数据库连接信息
$servername = "localhost";
$dbUsername = "a1sax1m9i";
$dbPassword = "a1sax1m9i";
$dbName = "a1sax1m9i";
// 创建连接
$conn = new mysqli($servername, $dbUsername, $dbPassword, $dbName);
if ($conn->connect_error) {
die("数据库连接失败: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
$message = "";
$messageType = "";
// 处理表单提交
/*
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST['action'] == 'add_announcement') {
$nr = trim($_POST["nr"] ?? "");
if (empty($nr)) {
$message = "公告内容不能为空!";
$messageType = "error";
} else {
$stmt = $conn->prepare("INSERT INTO announcements (nr) VALUES (?)");
$stmt->bind_param("s", $nr);
if ($stmt->execute()) {
$message = "公告发布成功!";
$messageType = "success";
header("Location: " . $_SERVER['PHP_SELF']);
exit;
} else {
$message = "发布失败: " . $conn->error;
$messageType = "error";
}
$stmt->close();
}
}*/
// 查询所有公告
$announcements = [];
$ggResult = $conn->query("SELECT id, nr, time FROM announcements ORDER BY id DESC");
while($row = $ggResult->fetch_assoc()) {
$announcements[] = $row;
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="zh_cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>历史公告</title>
<link rel="icon" href="./static/icon/icon.png" type="image/png">
<link rel="alternate icon" href="./static/icon/icon.ico" type="image/x-icon">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@font-face {
font-family: "MyCustomFont";
src: url("./static/font/bbc.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
:root {
--main-font: "MyCustomFont", sans-serif; /* 自定义字体变量 */
}
body {font-family: var(--main-font) /* 应用自定义字体 */, sans-serif; background-color: #f5f5f7; color: #333; margin: 0; padding: 20px; }
.container { max-width: 800px; margin: 0 auto; background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
h1, h2 { color: #2c3e50; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 8px; font-weight: bold; }
textarea.form-control { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; min-height: 120px; }
.submit-btn { background-color: #2c3e50; color: white; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; font-size: 16px; }
.submit-btn:hover { background-color: #34495e; }
.announcement-list { margin-top: 30px; }
.announcement-item { padding: 15px; border-bottom: 1px solid #eee; }
.announcement-item:last-child { border-bottom: none; }
.announcement-meta { color: #777; font-size: 14px; margin-bottom: 8px; }
.announcement-content { line-height: 1.6; }
.back-link { display: inline-block; margin-top: 20px; color: #2c3e50; text-decoration: none; }
.back-link:hover { text-decoration: underline; }
.message { padding: 10px; margin-bottom: 15px; border-radius: 4px; }
.success { background-color: #d4edda; color: #155724; }
.error { background-color: #f8d7da; color: #721c24; }
</style>
</head>
<body>
<div class="container">
<h1>历史公告</h1>
<!-- 发布公告表单 -->
<!--<div class="recommend-container">
<h2>发布新公告</h2>
<?php if (!empty($message)): ?>
<div class="message <?php echo $messageType; ?>">
<?php echo $message; // 提示信息仍用纯文本 ?>
</div>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<input type="hidden" name="action" value="add_announcement">
<div class="form-group">
<label for="nr">公告内容 (支持HTML)</label>
<textarea id="nr" name="nr" class="form-control" placeholder="请输入公告内容,例如:<b>加粗</b>、<a href='https://shanwogou.cn'>链接</a>..." required></textarea>
</div>
<button type="submit" class="submit-btn">发布公告</button>
</form>
</div>-->
<!-- 历史公告列表 -->
<div class="announcement-list">
<h2>历史公告</h2>
<?php if (empty($announcements)): ?>
<p>暂无历史公告。</p>
<?php else: ?>
<?php foreach($announcements as $ann): ?>
<div class="announcement-item">
<div class="announcement-meta">
<span>ID: <?php echo $ann['id']; ?></span> |
<span>发布时间: <?php echo $ann['time']; ?></span>
</div>
<div class="announcement-content">
<?php
// 核心修改:移除 htmlspecialchars让HTML标签生效
// 保留 nl2br 以正确处理换行符
echo nl2br($ann['nr']);
?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<a href="index.php" class="back-link"><i class="fas fa-arrow-left"></i> 返回首页</a>
</div>
</body>
</html>

2183
index.php Normal file

File diff suppressed because it is too large Load Diff

33
init_db.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
// 数据库连接参数
$host = 'localhost';
$rootUser = 'a1sax1m9i';
$rootPass = 'a1sax1m9i';
$dbname = 'a1sax1m9i';
try {
// 先连接到MySQL服务器不指定数据库
$pdo = new PDO("mysql:host=$host;charset=utf8", $rootUser, $rootPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 创建数据库
$pdo->exec("CREATE DATABASE IF NOT EXISTS $dbname");
// 连接到新创建的数据库
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $rootUser, $rootPass);
// 创建推荐表
$sql = "CREATE TABLE IF NOT EXISTS recommendations (
id INT AUTO_INCREMENT PRIMARY KEY,
song_name VARCHAR(255) NOT NULL,
artist_name VARCHAR(255) NOT NULL,
reason TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)";
$pdo->exec($sql);
echo "数据库和表创建成功!请关闭此页面,开始使用网站。";
} catch(PDOException $e) {
die("初始化失败: " . $e->getMessage());
}
?>

258
login.php Normal file
View File

@@ -0,0 +1,258 @@
<?php
// 开启错误显示以便调试
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// 启动会话
session_start();
// 检查用户是否已登录,如果已登录则重定向到首页
if (isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in'] === true) {
header('Location: index.php');
exit;
}
// 数据库连接配置 - 请与register.php使用相同的配置
$servername = "localhost";
// 虚拟主机数据库用户名格式通常为 "用户名_前缀"
$username = "a1sax1m9i"; // 必须与注册页面使用的用户名一致
$password = "a1sax1m9i"; // 必须与注册页面使用的密码一致
$dbname = "a1sax1m9i"; // 必须与注册页面使用的数据库名一致
// 初始化错误信息
$error = "";
// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取表单数据并过滤
$email = trim($_POST['email'] ?? '');
$userPassword = $_POST['password'] ?? '';
// 表单验证
if (empty($email)) {
$error = "邮箱不能为空";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "请输入有效的邮箱地址";
} elseif (empty($userPassword)) {
$error = "密码不能为空";
} else {
// 连接数据库 - 添加详细的连接信息输出用于调试
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查数据库连接
if ($conn->connect_error) {
// 显示当前使用的连接参数,帮助调试
$error = "数据库连接失败: " . $conn->connect_error .
"<br>使用的连接信息:<br>" .
"服务器: " . htmlspecialchars($servername) . "<br>" .
"用户名: " . htmlspecialchars($username) . "<br>" .
"数据库名: " . htmlspecialchars($dbname);
} else {
// 准备查询语句 - 根据实际表结构调整字段名
// 确保password_hash与你的数据库字段名一致
$sql = "SELECT id, username, email, password_hash FROM users WHERE email = ?";
$stmt = $conn->prepare($sql);
if (!$stmt) {
$error = "准备查询语句失败: " . $conn->error;
} else {
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
// 检查用户是否存在
if ($stmt->num_rows == 1) {
// 绑定结果到变量 - 确保与查询的字段顺序一致
$stmt->bind_result($userId, $username, $userEmail, $passwordHash);
$stmt->fetch();
// 验证密码
if (password_verify($userPassword, $passwordHash)) {
// 密码正确,设置会话变量
$_SESSION['user_logged_in'] = true;
$_SESSION['user_id'] = $userId;
$_SESSION['user_info'] = [
'username' => $username,
'email' => $userEmail
];
// 更新最后登录时间
$updateStmt = $conn->prepare("UPDATE users SET last_login_at = CURRENT_TIMESTAMP WHERE id = ?");
if ($updateStmt) {
$updateStmt->bind_param("i", $userId);
$updateStmt->execute();
$updateStmt->close();
}
// 重定向到首页
header('Location: index.php');
exit;
} else {
$error = "密码不正确";
}
} else {
$error = "该邮箱未注册";
}
$stmt->close();
}
$conn->close();
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户登录 - 音乐分享网站</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@font-face {
font-family: "MyCustomFont";
src: url("./static/font/bbc.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
:root{
--main-font: "MyCustomFont", sans-serif; /* 自定义字体变量 */
}
/* 基础样式 */
body {
font-family: var(--main-font); /* 应用自定义字体 */
max-width: 500px;
margin: 2rem auto;
padding: 0 1rem;
background-color: #f5f5f7;
}
.container {
background-color: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #2c3e50;
}
.form-group {
margin-bottom: 1.5rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #333;
}
input {
width: 100%;
padding: 0.8rem;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
font-size: 1rem;
}
button {
width: 100%;
padding: 0.8rem;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #34495e;
}
.error-message {
background-color: #f8d7da;
color: #721c24;
padding: 1rem;
border-radius: 5px;
margin-bottom: 1.5rem;
text-align: left;
}
.register-link {
text-align: center;
margin-top: 1.5rem;
}
.register-link a {
color: #2c3e50;
text-decoration: none;
}
.register-link a:hover {
text-decoration: underline;
}
.forgot-password {
text-align: right;
margin-bottom: 1rem;
}
.forgot-password a {
color: #2c3e50;
text-decoration: none;
font-size: 0.9rem;
}
.forgot-password a:hover {
text-decoration: underline;
}
.db-hint {
font-size: 0.9rem;
color: #666;
margin-top: 1rem;
padding: 1rem;
background-color: #f8f9fa;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1><i class="fas fa-sign-in-alt"></i> 用户登录</h1>
<?php if (!empty($error)): ?>
<div class="error-message">
<i class="fas fa-exclamation-circle"></i> <?php echo nl2br(htmlspecialchars($error)); ?>
</div>
<?php if (strpos($error, '数据库连接失败') !== false): ?>
<div class="db-hint">
<strong>检查步骤:</strong><br>
1. 确认数据库用户名、密码和数据库名与虚拟主机面板一致<br>
2. 确保与register.php使用完全相同的数据库配置<br>
3. 检查该用户是否有权限访问指定数据库
</div>
<?php endif; ?>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<div class="form-group">
<label for="email">邮箱</label>
<input type="email" id="email" name="email"
value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" id="password" name="password" required>
</div>
<div class="forgot-password">
<a href="forgot_password.php">忘记密码?</a>
</div>
<button type="submit">登录</button>
</form>
<div class="register-link">
还没有账号?<a href="register.php">立即注册</a>
</div>
</div>
</body>
</html>

495
play.php Normal file
View File

@@ -0,0 +1,495 @@
<?php
try {
// 从主页面获取音乐列表
$musicList = require_once __DIR__ . '/data/music.php';
// 获取要播放的音频ID
$audioId = $_GET['play'] ?? '';
$audioInfo = null;
// 查找对应的音频信息
foreach ($musicList as $music) {
if ($music['id'] === $audioId) {
$audioInfo = $music;
break;
}
}
// 如果找不到音频,跳转到主页面
if (!$audioInfo) {
header('Location: index.php');
exit;
}
} catch (Exception $e) {
// 处理错误,显示友好信息并链接到主页面
echo "<html><body>";
echo "<h1>出错了: " . htmlspecialchars($e->getMessage()) . "</h1>";
echo "<p><a href='index.php'>返回音乐主页</a></p>";
echo "</body></html>";
exit;
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($audioInfo['title']); ?> - 音乐播放</title>
<link rel="icon" href="./static/icon/new-icon.png" type="image/png">
<link rel="alternate icon" href="./static/icon/icon.ico" type="image/x-ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@font-face {
font-family: "MyCustomFont";
src: url("./static/font/bbc.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
:root {
--main-font: "MyCustomFont", sans-serif; /* 自定义字体变量 */
}
/* 基础样式 */
body {
font-family: var(--main-font);
margin: 0;
padding: 20px;
background-color: #f5f5f7;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 600px;
background-color: white;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
text-align: center;
}
h1 {
color: #2c3e50;
margin-bottom: 0.5rem;
}
.artist {
color: #666;
font-size: 1.1rem;
margin-bottom: 2rem;
font-style: italic;
}
.back-link {
display: inline-block;
margin-bottom: 2rem;
color: #2c3e50;
text-decoration: none;
font-size: 0.9rem;
transition: color 0.3s;
}
.back-link:hover {
color: #3498db;
text-decoration: underline;
}
.video-container {
position: relative;
width: 100%;
padding-top: 56.25%; /* 16:9比例 */
margin: 1rem 0;
overflow: hidden;
border-radius: 8px;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: none;
}
/* 自定义音频播放器样式 */
.custom-audio-player {
width: 100%;
margin: 1.5rem 0;
box-sizing: border-box;
background-color: #f8f9fa;
border-radius: 8px;
padding: 0.8rem;
display: flex;
align-items: center;
gap: 0.8rem;
flex-wrap: wrap;
}
.audio-controls {
display: flex;
align-items: center;
gap: 0.8rem;
}
.audio-button {
background: none;
border: none;
cursor: pointer;
color: #2c3e50;
font-size: 1.2rem;
width: 36px;
height: 36px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
}
.audio-button:hover {
background-color: rgba(44, 62, 80, 0.1);
}
.audio-button.active {
background-color: #2c3e50;
color: white;
}
.progress-container {
flex-grow: 1;
display: flex;
align-items: center;
gap: 0.5rem;
min-width: 200px;
}
.audio-progress {
flex-grow: 1;
height: 4px;
background-color: #ddd;
border-radius: 2px;
cursor: pointer;
position: relative;
}
.progress-fill {
height: 100%;
background-color: #2c3e50;
border-radius: 2px;
width: 0%;
transition: width 0.1s ease;
}
.progress-handle {
position: absolute;
width: 12px;
height: 12px;
border-radius: 50%;
background-color: #2c3e50;
top: 50%;
transform: translateY(-50%);
left: 0%;
display: none;
transition: left 0.1s ease;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
}
.audio-progress:hover .progress-handle {
display: block;
}
.time-display {
font-size: 0.8rem;
color: #666;
width: 40px;
text-align: center;
}
.time-separator {
font-size: 0.8rem;
color: #666;
width: 10px;
text-align: center;
}
.volume-control {
display: flex;
align-items: center;
gap: 0.5rem;
margin-left: 0.5rem;
min-width: 100px;
}
.volume-button {
background: none;
border: none;
cursor: pointer;
color: #2c3e50;
font-size: 1rem;
transition: all 0.2s ease;
}
.volume-slider {
width: 80px;
height: 4px;
-webkit-appearance: none;
appearance: none;
background: #ddd;
border-radius: 2px;
outline: none;
}
.volume-slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 12px;
height: 12px;
border-radius: 50%;
background: #2c3e50;
cursor: pointer;
}
.download-link {
display: inline-block;
margin-top: 1rem;
color: #2c3e50;
text-decoration: none;
padding: 0.5rem 1rem;
border: 1px solid #2c3e50;
border-radius: 20px;
transition: all 0.3s ease;
}
.download-link:hover {
background-color: #2c3e50;
color: white;
}
@media (max-width: 768px) {
.container {
padding: 1rem;
}
.volume-control {
margin-top: 0.5rem;
width: 100%;
justify-content: center;
order: 3;
}
.progress-container {
order: 2;
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<p>
<a href="index.php#download-<?php echo $audioInfo['id']; ?>" class="download-link" target="_blank">
<i class="fas fa-download"></i> 前往主站-查看更多音乐
</a>
</p>
<h1><?php echo htmlspecialchars($audioInfo['title']); ?></h1>
<p class="artist">作者: <?php echo htmlspecialchars($audioInfo['artist']); ?></p>
<div class="custom-audio-player">
<div class="audio-controls">
<button class="audio-button play-pause" data-audio="<?php echo $audioInfo['id']; ?>">▶</button>
<button class="audio-button loop" data-audio="<?php echo $audioInfo['id']; ?>">🔄</button>
</div>
<div class="progress-container">
<div class="audio-progress" data-audio="<?php echo $audioInfo['id']; ?>">
<div class="progress-fill" data-audio="<?php echo $audioInfo['id']; ?>"></div>
<div class="progress-handle" data-audio="<?php echo $audioInfo['id']; ?>"></div>
</div>
<span class="time-display current-time" data-audio="<?php echo $audioInfo['id']; ?>">0:00</span>
<span class="time-separator">/</span>
<span class="time-display total-time" data-audio="<?php echo $audioInfo['id']; ?>"><?php echo $audioInfo['duration']; ?></span>
</div>
<div class="volume-control" data-audio="<?php echo $audioInfo['id']; ?>">
<button class="volume-button" data-audio="<?php echo $audioInfo['id']; ?>">🔊</button>
<input type="range" class="volume-slider" data-audio="<?php echo $audioInfo['id']; ?>" min="0" max="1" step="0.05" value="1">
</div>
</div>
<audio id="<?php echo $audioInfo['id']; ?>" class="audio-element" src="<?php echo htmlspecialchars($audioInfo['mp3']); ?>" type="audio/mpeg">
您的浏览器不支持音频播放
</audio>
<div class="video-container">
<iframe
src="https://player.bilibili.com/player.html?bvid=<?php echo $music['bvid']; ?>&page=1"
allowfullscreen="true"
loading="lazy">
</iframe>
</div>
<div class="download-section">
<p>下载:
<a href="<?php echo htmlspecialchars($music['mp3']); ?>" class="download-link" download>点我下载</a>
</p>
</div>
</div>
<script>
// 音频播放器功能
(function(d, w) {
var q = d.querySelector.bind(d);
var addEvt = function(el, evt, fn) {
el.addEventListener(evt, fn);
};
// 初始化播放器
function initAudioPlayer() {
var audioId = "<?php echo $audioInfo['id']; ?>";
var audio = q('#' + audioId);
var playBtn = q('.play-pause[data-audio="' + audioId + '"]');
var loopBtn = q('.loop[data-audio="' + audioId + '"]');
var progressBar = q('.audio-progress[data-audio="' + audioId + '"]');
var progressFill = q('.progress-fill[data-audio="' + audioId + '"]');
var progressHandle = q('.progress-handle[data-audio="' + audioId + '"]');
var currentTime = q('.current-time[data-audio="' + audioId + '"]');
var totalTime = q('.total-time[data-audio="' + audioId + '"]');
var volumeBtn = q('.volume-button[data-audio="' + audioId + '"]');
var volumeSlider = q('.volume-slider[data-audio="' + audioId + '"]');
var originalVolume = 1;
var isDragging = false;
// 音频加载完成
addEvt(audio, 'loadedmetadata', function() {
totalTime.textContent = formatTime(audio.duration);
audio.volume = volumeSlider.value;
});
// 播放/暂停
addEvt(playBtn, 'click', function() {
if (audio.paused) {
audio.play();
playBtn.textContent = '⏸';
} else {
audio.pause();
playBtn.textContent = '▶';
}
});
// 循环播放
addEvt(loopBtn, 'click', function() {
audio.loop = !audio.loop;
loopBtn.classList.toggle('active', audio.loop);
});
// 更新进度
addEvt(audio, 'timeupdate', function() {
if (!isDragging) {
var progress = (audio.currentTime / audio.duration) * 100;
progressFill.style.width = progress + '%';
progressHandle.style.left = progress + '%';
currentTime.textContent = formatTime(audio.currentTime);
}
});
// 进度条拖动
var startDrag = function(e) {
isDragging = true;
updateProgress(e);
addEvt(d, 'mousemove', dragProgress);
addEvt(d, 'touchmove', dragProgress);
addEvt(d, 'mouseup', endDrag);
addEvt(d, 'touchend', endDrag);
};
var dragProgress = function(e) {
if (isDragging) updateProgress(e);
};
var endDrag = function() {
isDragging = false;
var rect = progressBar.getBoundingClientRect();
var pos = Math.min(Math.max(0, progressHandle.offsetLeft / rect.width), 1);
audio.currentTime = pos * audio.duration;
d.removeEventListener('mousemove', dragProgress);
d.removeEventListener('touchmove', dragProgress);
};
var updateProgress = function(e) {
var rect = progressBar.getBoundingClientRect();
var clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
var pos = (clientX - rect.left) / rect.width;
pos = Math.min(Math.max(0, pos), 1);
var percent = pos * 100;
progressFill.style.width = percent + '%';
progressHandle.style.left = percent + '%';
currentTime.textContent = formatTime(pos * audio.duration);
};
addEvt(progressBar, 'mousedown', startDrag);
addEvt(progressBar, 'touchstart', startDrag);
// 音量控制
addEvt(volumeSlider, 'input', function() {
audio.volume = this.value;
updateVolumeIcon();
if (audio.muted && this.value > 0) audio.muted = false;
});
// 静音切换
addEvt(volumeBtn, 'click', function() {
audio.muted = !audio.muted;
updateVolumeIcon();
if (!audio.muted) {
volumeSlider.value = originalVolume;
audio.volume = originalVolume;
} else {
originalVolume = volumeSlider.value;
volumeSlider.value = 0;
}
});
// 更新音量图标
function updateVolumeIcon() {
if (audio.muted || volumeSlider.value == 0) {
volumeBtn.textContent = '🔇';
} else if (volumeSlider.value < 0.5) {
volumeBtn.textContent = '🔈';
} else {
volumeBtn.textContent = '🔊';
}
}
// 播放结束
addEvt(audio, 'ended', function() {
playBtn.textContent = '▶';
});
}
// 格式化时间
function formatTime(seconds) {
if (isNaN(seconds)) return '0:00';
var mins = Math.floor(seconds / 60);
var secs = Math.floor(seconds % 60);
return mins + ':' + (secs < 10 ? '0' + secs : secs);
}
// 页面加载完成后初始化
addEvt(d, 'DOMContentLoaded', function() {
initAudioPlayer();
});
})(document, window);
</script>
</body>
</html>