feat(Splash): 添加视频自动播放处理逻辑
添加对微信环境下视频自动播放被阻止的处理,通过WeixinJSBridge尝试重新播放
Showing
2 changed files
with
27 additions
and
1 deletions
| ... | @@ -2,6 +2,7 @@ | ... | @@ -2,6 +2,7 @@ |
| 2 | <div class="splash-container" :class="{ 'fade-out': isExiting }"> | 2 | <div class="splash-container" :class="{ 'fade-out': isExiting }"> |
| 3 | <!-- 背景视频 --> | 3 | <!-- 背景视频 --> |
| 4 | <video | 4 | <video |
| 5 | + ref="videoPlayer" | ||
| 5 | class="background-video" | 6 | class="background-video" |
| 6 | autoplay | 7 | autoplay |
| 7 | muted | 8 | muted |
| ... | @@ -48,6 +49,7 @@ import { useRouter } from 'vue-router' | ... | @@ -48,6 +49,7 @@ import { useRouter } from 'vue-router' |
| 48 | 49 | ||
| 49 | const router = useRouter() | 50 | const router = useRouter() |
| 50 | const isExiting = ref(false) | 51 | const isExiting = ref(false) |
| 52 | +const videoPlayer = ref(null) | ||
| 51 | 53 | ||
| 52 | // TODO: 视频配置 | 54 | // TODO: 视频配置 |
| 53 | const videoUrl = ref('https://cdn.ipadbiz.cn/stdj/video/sample-10s.mp4') | 55 | const videoUrl = ref('https://cdn.ipadbiz.cn/stdj/video/sample-10s.mp4') |
| ... | @@ -62,7 +64,29 @@ const enterApp = () => { | ... | @@ -62,7 +64,29 @@ const enterApp = () => { |
| 62 | } | 64 | } |
| 63 | 65 | ||
| 64 | onMounted(() => { | 66 | onMounted(() => { |
| 65 | - // 可以在这里添加一些初始化逻辑 | 67 | + const video = videoPlayer.value |
| 68 | + if (video) { | ||
| 69 | + // 尝试播放视频 | ||
| 70 | + const playPromise = video.play() | ||
| 71 | + | ||
| 72 | + if (playPromise !== undefined) { | ||
| 73 | + playPromise.catch(error => { | ||
| 74 | + // 自动播放被阻止,在微信环境下尝试通过 WeixinJSBridge 播放 | ||
| 75 | + console.log('自动播放失败:', error) | ||
| 76 | + const ua = navigator.userAgent.toLowerCase() | ||
| 77 | + if (ua.match(/MicroMessenger/i) && typeof window.WeixinJSBridge !== 'undefined') { | ||
| 78 | + window.WeixinJSBridge.invoke('getNetworkType', {}, () => { | ||
| 79 | + video.play() | ||
| 80 | + }) | ||
| 81 | + } | ||
| 82 | + }) | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + // 监听微信JSBridgeReady事件 | ||
| 86 | + document.addEventListener('WeixinJSBridgeReady', () => { | ||
| 87 | + video.play() | ||
| 88 | + }, false) | ||
| 89 | + } | ||
| 66 | }) | 90 | }) |
| 67 | </script> | 91 | </script> |
| 68 | 92 | ... | ... |
-
Please register or login to post a comment