You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1070 lines
25 KiB

<template>
<view class="container">
<!-- 歌词显示区域 -->
<view class="lyrics-container">
<view class="lyrics-content">
<!-- 三句歌词显示 -->
<view class="three-sentences">
<!-- 上一句 -->
<view class="sentence-item prev-sentence">
<text class="lyric-line prev-line">
{{ prevSentence ? prevSentence.FinalSentence : "" }}
</text>
</view>
<!-- 当前句子 -->
<view class="sentence-item current-sentence">
<text class="lyric-line active">
{{ currentSentence ? currentSentence.FinalSentence : "" }}
</text>
</view>
<!-- 下一句 -->
<view class="sentence-item next-sentence">
<text class="lyric-line next-line">
{{ nextSentence ? nextSentence.FinalSentence : "" }}
</text>
</view>
</view>
<!-- 句子指示器 -->
<!-- <view class="sentence-indicator">
<text class="sentence-text">
{{ currentSentenceIndex + 1 }}/{{ totalSentences }}
</text>
</view> -->
</view>
</view>
<!-- 播放控制区域 -->
<view class="player-controls">
<!-- 控制按钮区域进度条上方 -->
<view class="control-icons" v-if="false">
<view class="control-icon">
<text class="icon-text"></text>
</view>
<view class="control-icon-group">
<view class="control-icon">
<text class="icon-text"></text>
</view>
<view class="control-icon">
<text class="icon-text"></text>
</view>
</view>
</view>
<!-- 进度条区域 -->
<view class="progress-section">
<text class="time-text">{{ formatTime(currentTime) }}</text>
<view class="progress-container">
<view class="progress-bar" @click="seek">
<view class="progress" :style="{ width: progress + '%' }"></view>
<view
class="progress-thumb"
:style="{ left: progress + '%' }"
></view>
</view>
</view>
<text class="time-text">{{ formatTime(duration) }}</text>
</view>
<!-- 点赞和评论区域 -->
<view class="interaction-section">
<view class="like-section">
<view class="like-btn" @click="toggleLike">
<text class="like-icon">{{ isLiked ? "❤️" : "" }}</text>
<text class="like-count">{{ likeCount }}</text>
</view>
<view class="comment-btn" @click="toggleComments">
<text class="comment-icon"></text>
<text class="comment-count">{{ comments.length }}</text>
</view>
</view>
</view>
<!-- 评论列表 -->
<view class="comments-container" v-if="showComments">
<view class="comments-header">
<text class="comments-title">评论 ({{ comments.length }})</text>
<view class="close-btn" @click="closeComments">
<text class="close-icon"></text>
</view>
</view>
<!-- 评论输入框 -->
<view class="comment-input-section">
<input
class="comment-input"
v-model="newComment"
placeholder="写下你的想法..."
@confirm="submitComment"
/>
<view
class="submit-btn"
@click="submitComment"
:class="{ disabled: !newComment.trim() }"
>
<text class="submit-text">发送</text>
</view>
</view>
<!-- 评论列表 -->
<scroll-view class="comments-list" scroll-y>
<view
class="comment-item"
v-for="(comment, index) in comments"
:key="index"
>
<view class="comment-avatar">
<text class="avatar-text">{{ comment.userName.charAt(0) }}</text>
</view>
<view class="comment-content">
<view class="comment-header">
<text class="comment-user">{{ comment.userName }}</text>
<text class="comment-time">{{
formatCommentTime(comment.createTime)
}}</text>
</view>
<text class="comment-text">{{ comment.content }}</text>
</view>
</view>
<!-- 空状态 -->
<view class="empty-comments" v-if="comments.length === 0">
<text class="empty-text">暂无评论,快来抢沙发吧~</text>
</view>
</scroll-view>
</view>
<!-- 底部控制按钮 -->
<view class="bottom-controls">
<view class="control-btn">
<text class="control-icon-text">⟲</text>
</view>
<view class="control-btn" @click="prevChapter">
<text class="control-icon-text">⏮</text>
</view>
<view class="control-btn play-btn" @click="togglePlay">
<text class="control-icon-text">{{ playing ? "⏸" : "▶" }}</text>
</view>
<view class="control-btn" @click="nextChapter">
<text class="control-icon-text">⏭</text>
</view>
<view class="control-btn">
<text class="control-icon-text"></text>
</view>
</view>
</view>
<AudioPlayer
ref="audioPlayer"
:src="currentChapter.audioUrl"
@timeupdate="onTimeUpdate"
@ended="onEnded"
></AudioPlayer>
</view>
</template>
<script>
// 由于uniapp不支持直接使用audio元素,我们使用innerAudioContext
import AudioPlayer from "../components/AudioPlayer.vue";
const msg = require("../components/msg.json");
console.log(msg);
export default {
components: {
AudioPlayer,
},
data() {
return {
chapters: [
{
title: "元界修真传",
audioUrl:
"https://des.dayunyuanjian.cn/data/2025/09/15/ecd0671f-ba8b-4b69-bb07-1a1d8b4bbfd0.MP3",
// 句子级别的时间同步数据,从msg.json的ResultDetail数组获取
sentences:
require("../components/msg.json")?.Response?.Data?.ResultDetail ||
[],
content:
require("../components/msg.json")?.Response?.Data?.Result || "",
},
],
currentChapterIndex: 0,
playing: false,
currentTime: 0,
duration: 0,
activeSentenceIndex: -1,
scrollTop: 0,
scrollViewHeight: 0,
// 句子相关数据
currentSentenceIndex: 0,
// 点赞和评论相关数据
isLiked: false,
likeCount: 0,
comments: [],
showComments: false,
newComment: "",
commentLoading: false,
};
},
mounted() {
this.syncAudioState();
this.calculateScrollViewHeight();
},
onShow() {
// 页面显示时同步音频状态
this.syncAudioState();
},
onHide() {
// 页面隐藏时保持音频播放状态
// 背景音频会自动继续播放
},
computed: {
currentChapter() {
const chapter = this.chapters[this.currentChapterIndex];
// 使用句子级别的时间同步数据
const sentences = chapter.sentences.map((sentence) => {
return {
...sentence,
startTimeInSeconds: sentence.StartMs / 1000, // 将毫秒转换为秒
endTimeInSeconds: sentence.EndMs / 1000, // 将毫秒转换为秒
};
});
return {
...chapter,
sentences: sentences,
};
},
progress() {
return (this.currentTime / this.duration) * 100 || 0;
},
currentTimeFormatted() {
return this.formatTime(this.currentTime);
},
durationFormatted() {
return this.formatTime(this.duration);
},
// 句子相关计算属性
totalSentences() {
return this.currentChapter.sentences.length;
},
currentSentence() {
return this.currentChapter.sentences[this.currentSentenceIndex] || null;
},
prevSentence() {
if (this.currentSentenceIndex > 0) {
return (
this.currentChapter.sentences[this.currentSentenceIndex - 1] || null
);
}
return null;
},
nextSentence() {
if (this.currentSentenceIndex < this.totalSentences - 1) {
return (
this.currentChapter.sentences[this.currentSentenceIndex + 1] || null
);
}
return null;
},
},
methods: {
togglePlay() {
if (this.playing) {
this.$refs.audioPlayer.pause();
this.playing = false;
} else {
this.$refs.audioPlayer.play();
this.playing = true;
}
},
prevChapter() {
if (this.currentChapterIndex > 0) {
this.currentChapterIndex--;
this.resetPlayback();
}
},
nextChapter() {
if (this.currentChapterIndex < this.chapters.length - 1) {
this.currentChapterIndex++;
this.resetPlayback();
}
},
resetPlayback() {
this.playing = false;
this.currentTime = 0;
this.duration = 0;
this.activeSentenceIndex = -1;
this.currentSentenceIndex = 0;
// 更新背景音频信息
const currentChapter = this.chapters[this.currentChapterIndex];
if (
this.$refs.audioPlayer &&
this.$refs.audioPlayer.backgroundAudioManager
) {
const bgAudio = this.$refs.audioPlayer.backgroundAudioManager;
bgAudio.title = currentChapter.title;
bgAudio.epname = `${this.currentChapterIndex + 1}`;
bgAudio.src = currentChapter.audioUrl;
// 确保音频从头开始播放
bgAudio.currentTime = 0;
// 如果音频播放器有seek方法,也调用它来确保重置
if (this.$refs.audioPlayer.seek) {
this.$refs.audioPlayer.seek(0);
}
}
},
onTimeUpdate(e) {
this.currentTime = e.currentTime;
this.duration = e.duration;
// 同步播放状态
this.playing = !e.paused;
// 句子级别的高亮同步
this.updateSentenceHighlight(this.currentTime);
},
onEnded() {
this.playing = false;
// 自动播放下一章节
if (this.currentChapterIndex < this.chapters.length - 1) {
this.nextChapter();
setTimeout(() => {
this.$refs.audioPlayer.play();
this.playing = true;
}, 500);
}
},
seek(e) {
// 使用uni.createSelectorQuery获取进度条信息
const query = uni.createSelectorQuery().in(this);
query
.select(".progress-bar")
.boundingClientRect((rect) => {
if (rect) {
const clickX = e.detail.x - rect.left;
const progressPercent = clickX / rect.width;
const seekTime = progressPercent * this.duration;
if (seekTime >= 0 && seekTime <= this.duration) {
this.$refs.audioPlayer.seek(seekTime);
// 手动拖动后同步句子高亮和滚动位置
this.updateSentenceHighlight(seekTime);
}
}
})
.exec();
},
updateSentenceHighlight(currentTimeSeconds) {
// 根据指定时间找到对应的句子并更新当前句子
const sentences = this.currentChapter.sentences;
let targetIndex = -1;
// 精确匹配:找到当前时间对应的句子
for (let i = 0; i < sentences.length; i++) {
const sentence = sentences[i];
if (
currentTimeSeconds >= sentence.startTimeInSeconds &&
currentTimeSeconds <= sentence.endTimeInSeconds
) {
targetIndex = i;
break;
}
}
// 如果没有精确匹配,尝试找到最接近的句子
if (targetIndex === -1 && sentences.length > 0) {
// 找到时间最接近的句子,而不是简单的比例估算
let minDistance = Infinity;
for (let i = 0; i < sentences.length; i++) {
const sentence = sentences[i];
const sentenceMiddle =
(sentence.startTimeInSeconds + sentence.endTimeInSeconds) / 2;
const distance = Math.abs(currentTimeSeconds - sentenceMiddle);
if (distance < minDistance) {
minDistance = distance;
targetIndex = i;
}
}
}
// 只有当索引真正发生变化时才更新
if (targetIndex >= 0 && targetIndex !== this.activeSentenceIndex) {
this.activeSentenceIndex = targetIndex;
this.currentSentenceIndex = targetIndex;
}
},
formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, "0")}:${secs
.toString()
.padStart(2, "0")}`;
},
syncAudioState() {
// 同步背景音频状态到界面
if (
this.$refs.audioPlayer &&
this.$refs.audioPlayer.backgroundAudioManager
) {
const bgAudio = this.$refs.audioPlayer.backgroundAudioManager;
// 检查当前是否有音频在播放
if (bgAudio.src) {
// 同步播放状态 - 注意:paused为true表示暂停,false表示播放
this.playing = !bgAudio.paused;
// 同步时间信息
this.currentTime = bgAudio.currentTime || 0;
this.duration = bgAudio.duration || 0;
// 如果有音频在播放,启动时间更新
if (this.playing) {
this.$refs.audioPlayer.startTimeUpdate();
}
} else {
// 没有音频源时重置状态
this.playing = false;
this.currentTime = 0;
this.duration = 0;
}
}
},
calculateScrollViewHeight() {
// 获取系统信息
const systemInfo = uni.getSystemInfoSync();
const windowHeight = systemInfo.windowHeight;
// 计算控制栏高度(大约300rpx转换为px)
const controlsHeight = 300 * (systemInfo.windowWidth / 750);
// 计算内容区域可用高度
this.scrollViewHeight = windowHeight - controlsHeight - 60; // 60px为额外边距
},
// 点赞和评论相关方法
toggleLike() {
this.isLiked = !this.isLiked;
this.likeCount += this.isLiked ? 1 : -1;
// 这里可以调用API保存点赞状态
this.saveLikeStatus();
},
toggleComments() {
this.showComments = !this.showComments;
if (this.showComments && this.comments.length === 0) {
this.loadComments();
}
},
closeComments() {
this.showComments = false;
},
async loadComments() {
try {
// 模拟加载评论数据
// 实际项目中这里应该调用API
this.comments = [
{
id: 1,
userName: "用户1",
content: "这首歌真的很好听!",
createTime: new Date().getTime() - 1000 * 60 * 30, // 30分钟前
},
{
id: 2,
userName: "音乐爱好者",
content: "歌词写得很有意境",
createTime: new Date().getTime() - 1000 * 60 * 60 * 2, // 2小时前
},
{
id: 3,
userName: "文艺青年",
content: "每次听都有不同的感受",
createTime: new Date().getTime() - 1000 * 60 * 60 * 24, // 1天前
},
{
id: 1,
userName: "用户1",
content: "这首歌真的很好听!",
createTime: new Date().getTime() - 1000 * 60 * 30, // 30分钟前
},
{
id: 2,
userName: "音乐爱好者",
content: "歌词写得很有意境",
createTime: new Date().getTime() - 1000 * 60 * 60 * 2, // 2小时前
},
{
id: 3,
userName: "文艺青年",
content: "每次听都有不同的感受",
createTime: new Date().getTime() - 1000 * 60 * 60 * 24, // 1天前
},
];
} catch (error) {
console.error("加载评论失败:", error);
uni.showToast({
title: "加载评论失败",
icon: "none",
});
}
},
async submitComment() {
if (!this.newComment.trim()) {
uni.showToast({
title: "请输入评论内容",
icon: "none",
});
return;
}
this.commentLoading = true;
try {
// 模拟提交评论
const newComment = {
id: Date.now(),
userName: "我",
content: this.newComment.trim(),
createTime: new Date().getTime(),
};
this.comments.unshift(newComment);
this.newComment = "";
uni.showToast({
title: "评论成功",
icon: "success",
});
// 这里可以调用API保存评论
this.saveComment(newComment);
} catch (error) {
console.error("提交评论失败:", error);
uni.showToast({
title: "评论失败",
icon: "none",
});
} finally {
this.commentLoading = false;
}
},
formatCommentTime(timestamp) {
const now = new Date().getTime();
const diff = now - timestamp;
if (diff < 1000 * 60) {
return "刚刚";
} else if (diff < 1000 * 60 * 60) {
return Math.floor(diff / (1000 * 60)) + "分钟前";
} else if (diff < 1000 * 60 * 60 * 24) {
return Math.floor(diff / (1000 * 60 * 60)) + "小时前";
} else {
return Math.floor(diff / (1000 * 60 * 60 * 24)) + "天前";
}
},
async saveLikeStatus() {
// 实际项目中调用API保存点赞状态
console.log("保存点赞状态:", this.isLiked);
},
async saveComment(comment) {
// 实际项目中调用API保存评论
console.log("保存评论:", comment);
},
},
};
</script>
<style>
.container {
height: 100vh;
background: #f5f5f5;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
position: relative;
display: flex;
flex-direction: column;
}
/* 顶部标签样式 */
.top-label {
display: flex;
justify-content: center;
padding: 40rpx 0 20rpx 0;
}
.label-content {
background: #e8e8e8;
border: 2rpx solid #d0d0d0;
border-radius: 40rpx;
padding: 16rpx 32rpx;
display: flex;
align-items: center;
gap: 16rpx;
}
.label-number {
width: 32rpx;
height: 32rpx;
background: #666;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 24rpx;
font-weight: 600;
}
.label-text {
color: #666;
font-size: 28rpx;
font-weight: 500;
}
/* 歌词容器样式 */
.lyrics-container {
flex: 1;
padding: 0 60rpx;
margin-bottom: 20rpx;
display: flex;
flex-direction: column;
justify-content: center;
}
.lyrics-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
position: relative;
}
.three-sentences {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
gap: 20rpx;
position: relative;
overflow: hidden;
}
.sentence-item {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
transition: all 0.3s ease;
}
.current-sentence {
transform: scale(1.05);
z-index: 2;
}
.prev-sentence {
opacity: 0.5;
transform: scale(0.85) translateY(-10rpx);
}
.next-sentence {
opacity: 0.5;
transform: scale(0.85) translateY(10rpx);
}
.lyric-line {
display: block;
text-align: center;
line-height: 1.6;
margin: 0;
transition: all 0.3s ease;
padding: 16rpx 20rpx;
word-break: break-all;
}
.lyric-line.active {
color: #333;
font-weight: 600;
font-size: 36rpx;
}
.lyric-line.prev-line {
color: #999;
font-size: 28rpx;
font-weight: 400;
}
.lyric-line.next-line {
color: #999;
font-size: 28rpx;
font-weight: 400;
}
.sentence-indicator {
position: absolute;
bottom: 20rpx;
right: 20rpx;
background: rgba(0, 0, 0, 0.1);
padding: 8rpx 16rpx;
border-radius: 20rpx;
}
.sentence-text {
font-size: 24rpx;
color: #666;
font-weight: 500;
}
/* 播放控制区域 */
.player-controls {
background: white;
padding: 40rpx 60rpx 60rpx 60rpx;
display: flex;
flex-direction: column;
gap: 24rpx;
}
/* 控制图标区域 */
.control-icons {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20rpx;
}
.control-icon {
width: 60rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: #f0f0f0;
}
.control-icon-group {
display: flex;
gap: 40rpx;
}
.icon-text {
font-size: 32rpx;
color: #666;
}
/* 进度条区域 */
.progress-section {
display: flex;
align-items: center;
gap: 24rpx;
margin-bottom: 16rpx;
}
.time-text {
font-size: 24rpx;
color: #666;
font-weight: 500;
font-family: "Courier New", monospace;
min-width: 80rpx;
text-align: center;
}
.progress-container {
flex: 1;
position: relative;
}
.progress-bar {
height: 8rpx;
background: #e0e0e0;
border-radius: 4rpx;
position: relative;
cursor: pointer;
}
.progress {
height: 100%;
background: #333;
border-radius: 4rpx;
transition: width 0.2s ease;
}
.progress-thumb {
position: absolute;
top: 50%;
width: 24rpx;
height: 24rpx;
background: #333;
border-radius: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
}
/* 无损标识 */
.quality-label {
display: flex;
justify-content: center;
margin-bottom: 20rpx;
}
.quality-text {
font-size: 24rpx;
color: #999;
}
/* 点赞和评论区域 */
.interaction-section {
padding: 20rpx 0;
border-top: 1rpx solid #f0f0f0;
}
.like-section {
display: flex;
justify-content: center;
align-items: center;
gap: 60rpx;
}
.like-btn,
.comment-btn {
display: flex;
flex-direction: column;
align-items: center;
gap: 8rpx;
padding: 16rpx 24rpx;
border-radius: 20rpx;
background: #f8f8f8;
transition: all 0.3s ease;
}
.like-btn:active,
.comment-btn:active {
transform: scale(0.95);
background: #e8e8e8;
}
.like-icon,
.comment-icon {
font-size: 32rpx;
line-height: 1;
}
.like-count,
.comment-count {
font-size: 24rpx;
color: #666;
font-weight: 500;
}
/* 评论容器 */
.comments-container {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 60vh;
background: white;
border-radius: 32rpx 32rpx 0 0;
box-shadow: 0 -8rpx 32rpx rgba(0, 0, 0, 0.1);
z-index: 1000;
display: flex;
flex-direction: column;
}
.comments-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 32rpx 40rpx 20rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.comments-title {
font-size: 32rpx;
font-weight: 600;
color: #333;
}
.close-btn {
width: 60rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: #f0f0f0;
}
.close-icon {
font-size: 28rpx;
color: #666;
}
/* 评论输入区域 */
.comment-input-section {
display: flex;
align-items: center;
padding: 20rpx 40rpx;
gap: 20rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.comment-input {
flex: 1;
height: 80rpx;
background: #f8f8f8;
border-radius: 40rpx;
padding: 0 24rpx;
font-size: 28rpx;
border: none;
outline: none;
}
.submit-btn {
padding: 16rpx 32rpx;
background: #007aff;
border-radius: 40rpx;
transition: all 0.3s ease;
}
.submit-btn.disabled {
background: #ccc;
}
.submit-text {
color: white;
font-size: 28rpx;
font-weight: 500;
}
/* 评论列表 */
.comments-list {
flex: 1;
padding: 20rpx 40rpx;
width: 680rpx;
height: 400rpx;
}
.comment-item {
display: flex;
gap: 20rpx;
padding: 24rpx 0;
border-bottom: 1rpx solid #f8f8f8;
}
.comment-item:last-child {
border-bottom: none;
}
.comment-avatar {
width: 80rpx;
height: 80rpx;
border-radius: 50%;
background: linear-gradient(135deg, #667eea, #764ba2);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.avatar-text {
color: white;
font-size: 32rpx;
font-weight: 600;
}
.comment-content {
flex: 1;
display: flex;
flex-direction: column;
gap: 12rpx;
}
.comment-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.comment-user {
font-size: 28rpx;
font-weight: 600;
color: #333;
}
.comment-time {
font-size: 24rpx;
color: #999;
}
.comment-text {
font-size: 28rpx;
color: #333;
line-height: 1.6;
}
/* 空状态 */
.empty-comments {
display: flex;
justify-content: center;
align-items: center;
height: 200rpx;
}
.empty-text {
font-size: 28rpx;
color: #999;
}
/* 底部控制按钮 */
.bottom-controls {
display: flex;
justify-content: center;
align-items: center;
gap: 60rpx;
}
.control-btn {
width: 80rpx;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: transparent;
cursor: pointer;
transition: all 0.2s ease;
}
.control-btn.play-btn {
background: #333;
color: white;
}
.control-icon-text {
font-size: 32rpx;
color: #666;
line-height: 1;
}
.control-btn.play-btn .control-icon-text {
color: white;
font-size: 36rpx;
}
.control-btn:active {
transform: scale(0.95);
}
.control-btn:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.control-btn:disabled:active {
transform: none;
}
</style>