1、增加背景音乐播放和控制,mp3改为采用动态方式加载,避免浏览器缓存
2、前门模块的鼓声同样处理
This commit is contained in:
Wenzhe 2026-02-04 00:01:32 +08:00
parent e3d0ffe1e6
commit d89ce10c34
3 changed files with 117 additions and 1 deletions

View File

@ -82,7 +82,8 @@ const toggleMusic = () => {
if (!musicPlayer.value) { if (!musicPlayer.value) {
// //
musicPlayer.value = uni.createInnerAudioContext() musicPlayer.value = uni.createInnerAudioContext()
musicPlayer.value.src = '/static/music/bgm1.mp3' const bgmUrl = new URL('/static/music/bgm1.mp3', import.meta.url)
musicPlayer.value.src = bgmUrl.href
musicPlayer.value.loop = false musicPlayer.value.loop = false
// //

View File

@ -57,6 +57,10 @@ const isAutoScrolling = ref(false)
const showLoading = ref(true) const showLoading = ref(true)
// //
const isImagesLoaded = ref(false) const isImagesLoaded = ref(false)
//
const isMusicPlaying = ref(false)
//
const audioPlayer = ref(null)
// //
@ -261,9 +265,62 @@ const initPage = () => {
console.log('页面准备就绪') console.log('页面准备就绪')
} }
//
const initMusicPlayer = () => {
try {
//
audioPlayer.value = uni.createInnerAudioContext()
const bgmUrl = new URL('/static/music/bgm1.mp3', import.meta.url)
audioPlayer.value.src = bgmUrl.href
audioPlayer.value.loop = true
//
audioPlayer.value.onEnded(() => {
audioPlayer.value.seek(0)
audioPlayer.value.play()
})
//
audioPlayer.value.onError((err) => {
console.error('音乐播放错误:', err)
})
console.log('音乐播放器初始化完成')
} catch (error) {
console.error('初始化音乐播放器失败:', error)
}
}
// /
const toggleMusic = () => {
if (!audioPlayer.value) {
initMusicPlayer()
}
if (isMusicPlaying.value) {
//
audioPlayer.value.pause()
isMusicPlaying.value = false
console.log('音乐已暂停')
} else {
//
audioPlayer.value.play()
isMusicPlaying.value = true
console.log('音乐已开始播放')
}
}
// //
onMounted(() => { onMounted(() => {
console.log('SinglePageContainer 组件挂载') console.log('SinglePageContainer 组件挂载')
//
initMusicPlayer()
//
setTimeout(() => {
toggleMusic()
}, 1000)
}) })
// //
@ -505,6 +562,15 @@ onUnmounted(() => {
<template> <template>
<div class="single-page-wrapper"> <div class="single-page-wrapper">
<!-- 音乐控制按钮 -->
<div
class="music-control"
:class="{ 'music-playing': isMusicPlaying }"
@click="toggleMusic"
>
<img src="/static/images/music_on.png" alt="音乐" class="music-icon" />
</div>
<!-- 加载组件 --> <!-- 加载组件 -->
<LoadingComponent <LoadingComponent
v-if="showLoading" v-if="showLoading"
@ -832,6 +898,43 @@ onUnmounted(() => {
} }
} }
/* 音乐控制按钮样式 */
.music-control {
position: fixed;
top: 20px;
right: 20px;
width: 50px;
height: 50px;
border-radius: 50%;
/* background-color: rgba(0, 0, 0, 0.3); */
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
cursor: pointer;
}
.music-icon {
width: 49rpx;
height: 49rpx;
transition: transform 0.3s ease;
}
/* 音乐播放时的转动动画 */
.music-playing .music-icon {
animation: rotate 2s linear infinite;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/* 响应式设计 */ /* 响应式设计 */
@media (max-width: 480px) { @media (max-width: 480px) {
.scene-title { .scene-title {
@ -850,5 +953,17 @@ onUnmounted(() => {
margin-right: 20px; margin-right: 20px;
margin-left: 20px; margin-left: 20px;
} }
.music-control {
width: 40px;
height: 40px;
top: 10px;
right: 10px;
}
.music-icon {
width: 24px;
height: 24px;
}
} }
</style> </style>

BIN
static/images/music_on.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB