feat:开屏页视频播放优化

This commit is contained in:
2026-05-28 14:39:00 +08:00
parent 7a75f8b4bf
commit 864f9ce627
+138 -26
View File
@@ -5,27 +5,34 @@
class="bg" class="bg"
:src="bgSrc" :src="bgSrc"
autoplay autoplay
loop
muted muted
:controls="false" :controls="false"
:show-fullscreen-btn="false" :show-fullscreen-btn="false"
:show-play-btn="false" :show-play-btn="false"
:show-center-play-btn="false" :show-center-play-btn="false"
:enable-progress-gesture="false" :enable-progress-gesture="false"
:show-loading="true"
playsinline
webkit-playsinline
object-fit="cover" object-fit="cover"
@error="onMediaError" @error="onMediaError"
@loadedmetadata="onVideoMetadata"
@canplay="onVideoReady"
@play="onVideoReady"
@ended="onVideoEnded"
@click="onMediaClick" @click="onMediaClick"
/> />
<image <image
v-else-if="bgSrc" v-else-if="bgSrc && !isVideo"
class="bg" class="bg"
:src="bgSrc" :src="bgSrc"
mode="aspectFill" mode="aspectFill"
@error="onMediaError" @error="onMediaError"
@load="onImageLoaded"
@click="onMediaClick" @click="onMediaClick"
/> />
<view class="count-down" :class="{ enabled: canEnter }" @click.stop="onEnterClick"> <view class="count-down" @click.stop="toHome">
{{ canEnter ? '进入' : `跳过 ${countDown}s` }} {{ `跳过 ${countDown}s` }}
</view> </view>
</view> </view>
</template> </template>
@@ -33,9 +40,32 @@
import { ref, onMounted, onUnmounted } from 'vue' import { ref, onMounted, onUnmounted } from 'vue'
import * as IndexApi from '@/sheep/api/index/index' import * as IndexApi from '@/sheep/api/index/index'
const DEFAULT_IMAGE_SECONDS = 3
const MAX_VIDEO_SECONDS = 15
const bgSrc = ref('') const bgSrc = ref('')
const jumpUrl = ref('') const jumpUrl = ref('')
const isVideo = ref(false) const isVideo = ref(false)
const mediaReady = ref(false)
let downloadToken = 0
let readyFallbackTimer = null
let countDownTimer = null
let jumpTimer = null
const normalizeAppFileUrl = (path) => {
const p = String(path || '')
if (!p) return ''
if (/^https?:\/\//i.test(p)) return p
if (p.startsWith('file://')) return p
try {
if (typeof plus !== 'undefined' && plus?.io?.convertLocalFileSystemURL) {
const next = plus.io.convertLocalFileSystemURL(p)
return next || p
}
} catch (e) {}
if (p.startsWith('/')) return `file://${p}`
return p
}
const isVideoUrl = (url) => { const isVideoUrl = (url) => {
const u = String(url || '') const u = String(url || '')
@@ -52,9 +82,32 @@ const fetchStartPage = async () => {
const media = String(data?.picUrl || '').trim() const media = String(data?.picUrl || '').trim()
const url = String(data?.url || '').trim() const url = String(data?.url || '').trim()
if (!media) return false if (!media) return false
bgSrc.value = media
jumpUrl.value = url jumpUrl.value = url
isVideo.value = isVideoUrl(media) isVideo.value = isVideoUrl(media)
if (isVideo.value && typeof plus !== 'undefined') {
const token = (downloadToken += 1)
const downloadPromise = new Promise((resolve) => {
uni.downloadFile({
url: media,
success: (r) => resolve(r?.statusCode === 200 ? r.tempFilePath : ''),
fail: () => resolve(''),
})
})
downloadPromise.then((localPath) => {
const normalized = normalizeAppFileUrl(localPath)
if (!normalized) return
if (token !== downloadToken) return
if (mediaReady.value) return
bgSrc.value = normalized
})
const fastLocal = await Promise.race([
downloadPromise,
new Promise((resolve) => setTimeout(() => resolve(''), 1500)),
])
bgSrc.value = normalizeAppFileUrl(fastLocal) || media
return true
}
bgSrc.value = media
return true return true
} catch (e) { } catch (e) {
return false return false
@@ -64,33 +117,100 @@ const fetchStartPage = async () => {
function onMediaError() { function onMediaError() {
bgSrc.value = '' bgSrc.value = ''
isVideo.value = false isVideo.value = false
mediaReady.value = false
startCountDown(DEFAULT_IMAGE_SECONDS)
} }
const countDown = ref(3) const countDown = ref(DEFAULT_IMAGE_SECONDS)
let timer = null
const canEnter = ref(false)
onMounted(() => { function clearTimers() {
fetchStartPage() if (readyFallbackTimer) clearTimeout(readyFallbackTimer)
timer = setInterval(() => { readyFallbackTimer = null
if (countDownTimer) clearInterval(countDownTimer)
countDownTimer = null
if (jumpTimer) clearTimeout(jumpTimer)
jumpTimer = null
}
function startCountDown(seconds) {
const s = Math.max(1, Math.floor(Number(seconds) || 0))
countDown.value = s
if (countDownTimer) clearInterval(countDownTimer)
countDownTimer = setInterval(() => {
if (countDown.value <= 1) { if (countDown.value <= 1) {
//点击进入 if (countDownTimer) clearInterval(countDownTimer)
// countDown.value = 0 countDownTimer = null
// canEnter.value = true
clearInterval(timer)
//直接进入
toHome() toHome()
return return
} }
countDown.value -= 1 countDown.value -= 1
}, 1000) }, 1000)
}
function scheduleJumpAfter(seconds) {
const ms = Math.max(0, Math.floor(Number(seconds) * 1000))
if (jumpTimer) clearTimeout(jumpTimer)
jumpTimer = setTimeout(() => {
toHome()
}, ms)
}
function onImageLoaded() {
if (mediaReady.value) return
mediaReady.value = true
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
}
function onVideoReady() {
if (!mediaReady.value) {
mediaReady.value = true
}
if (!countDownTimer) {
startCountDown(MAX_VIDEO_SECONDS)
scheduleJumpAfter(MAX_VIDEO_SECONDS + 0.2)
}
}
function onVideoMetadata(e) {
if (!mediaReady.value) {
mediaReady.value = true
}
const duration = Number(e?.detail?.duration || e?.detail?.totalTime || 0)
const limit = duration > 0 ? Math.min(MAX_VIDEO_SECONDS, Math.ceil(duration)) : MAX_VIDEO_SECONDS
startCountDown(limit)
scheduleJumpAfter(limit + 0.2)
}
function onVideoEnded() {
toHome()
}
onMounted(() => {
fetchStartPage().then((ok) => {
if (!ok) {
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
return
}
readyFallbackTimer = setTimeout(() => {
if (countDownTimer) return
if (isVideo.value) {
onVideoReady()
return
}
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
}, isVideo.value ? 2500 : 1200)
})
}) })
onUnmounted(() => { onUnmounted(() => {
if (timer) clearInterval(timer) clearTimers()
}) })
function toHome() { function toHome() {
clearTimers()
uni.reLaunch({ uni.reLaunch({
url: '/pages/index/index', url: '/pages/index/index',
}) })
@@ -110,11 +230,6 @@ function onMediaClick() {
return return
} }
} }
function onEnterClick() {
if (!canEnter.value) return
toHome()
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.start-page { .start-page {
@@ -141,9 +256,6 @@ function onEnterClick() {
line-height: 1; line-height: 1;
color: #fff; color: #fff;
background: rgba(0, 0, 0, 0.4); background: rgba(0, 0, 0, 0.4);
z-index: 10; z-index: 9999;
}
.count-down.enabled {
// background: rgba(54, 110, 238, 0.85);
} }
</style> </style>